-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamicMem.c
More file actions
55 lines (41 loc) · 962 Bytes
/
dynamicMem.c
File metadata and controls
55 lines (41 loc) · 962 Bytes
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
#include <stdio.h>
#include <stdlib.h>
int* func1(void);
int* func2(void);
int main(int argc, char const *argv[])
{
int* p = func1();
printf("%p\n", p);
printf("Deine Zahl auf dem Heap ist: %d\n", *p);
free(p);
//Array im Heap
int* arr_p = func2(); //Liefert Anfangsadresse auf den Stück Speicher im Heap für 20 Integer
printf("Prüfe ob korrekt mit -1 initialisiert: %d\n", *arr_p);
for(int i = 0; i < 20; i++)
{
arr_p[i] = i;
}
for(int i = 0; i < 20; i++)
{
printf("%d,", arr_p[i]);
}
printf("\n");
free(arr_p);
return 0;
}
int* func1(){
int a = 23;
int *p = (int*)malloc(sizeof(int));
printf("%p\n", p);
*p = a;
return p;
}
int* func2(){
//Allokiert für 20 Integer Speicher im Heap
int* arr_p = (int*)malloc(sizeof(int) * 20);
//Initialize
for(int i = 0; i < 20; i++){
arr_p[i] = -1;
}
return arr_p;
}