-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmalcfree.cpp
More file actions
29 lines (26 loc) · 767 Bytes
/
Copy pathmalcfree.cpp
File metadata and controls
29 lines (26 loc) · 767 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
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *p = (int *)malloc(sizeof(int));
printf("address=%p, value=%d\n", p, *p);
int *a = (int *)malloc(4 * sizeof(int));
a[0] = 1;
a[2] = 33;
// printf("%d \n %p", a[2], a);
struct MyStruct
{
int i;
float f;
char c[3];
};
MyStruct *my = (MyStruct *)calloc(4, sizeof(MyStruct));
my[1].i = 12;
my[0].f = 2.2;
printf("\n %p \n %d", my + 2, my[1].i);
return 0;
}
/*At compile time, only the space for the pointer is reserved (on the stack).
When the pointer is initialized, a block of memory of sizeof(int) bytes is allocated (on the heap) at program runtime.
The pointer on the stack then points to this memory location on the heap.
*/