-
Notifications
You must be signed in to change notification settings - Fork 0
/
023-malloc.cpp
60 lines (42 loc) · 1.48 KB
/
023-malloc.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <stdio.h>
#include <stdlib.h>
int main() {
// Sometimes we want to dynamically set the size of array
// Ide o dynamicke allocovanie pamate. Funkcie su definovane v stdlib.h
int *arr, *arr2;
// Funkcia malloc ocokava na vstupe velkost v bytoch kolko pamate ma alokovat
// Fukcia malloc vracia
arr = (int*)malloc(10 * sizeof(int));
// Malloc nenuluje byty a spravanie je nepredvidatelne
for (int i = 0; i < 10; i++) {
printf("%d\n", arr[i]);
}
// Funkcia Calloc nastavi vsetky bity na 0
arr2 = (int*)calloc(10, sizeof(int));
for (int i = 0; i < 10; i++)
{
printf("%d\n", arr2[i]);
}
// Nezabudnite uvolnit pamat na konci. Predidete memory leakom
// Priklad nacitania n cisel. Kde n je nacitane z klavesnice
int *nums, count;
printf("Zadajte pocet cisel: ");
scanf("%d", &count);
nums = (int*)malloc(count * sizeof(int));
for (int i = 0; i < count; i++) {
printf("Zadajce cislo %d: ", i + 1);
scanf("%d", &nums[i]);
}
for (int i = 0; i < count; i++) {
printf("%d\n", *(nums + i));
}
int size_nums = sizeof(nums)/sizeof(nums[0]);
printf("Size is %d\n", size_nums);
// nums[100] = 1000; // this could be dangerous
nums = (int*)realloc(nums, 100);
// Na pozadi sa c snazi allokovat nove bloky za uz alokovanymi blokmi. ak tam nie je dost miest C spusti malloc na novom mieste.
free(nums);
free(arr);
free(arr2);
return 0;
}