typedef struct{
int n1;
float n2;
char st[10];
} contact;
int main(){
contact fred;
printf("Contact address:\t%p\n", &fred);
printf("Contact n1 location:\t%p\n", &fred.n1);
printf("Contact n2 location:\t%p\n", &fred.n2);
printf("Contact nst location:\t%p\n", &fred.st);
return 0;
}
$ gcc main.c -o stack
$ ./stack
Contact address: 0x7ffcc8568b40
Contact n1 location: 0x7ffcc8568b40
Contact n2 location: 0x7ffcc8568b44
Contact nst location: 0x7ffcc8568b48
fred
)malloc does the same thing, but on the heap
typedef struct{
int n1;
float n2;
char st[10];
} contact;
int main(){
contact* fred = malloc(sizeof(*fred));
printf("Contact address:\t%p\n", fred);
printf("Contact n1 location:\t%p\n", &fred->n1);
printf("Contact n2 location:\t%p\n", &fred->n2);
printf("Contact nst location:\t%p\n", &fred->st);
free(fred);
return 0;
}
$ gcc malloc-main.c -o malloc-main
$ ./malloc-main
Contact address: 0x1bcd010
Contact n1 location: 0x1bcd010
Contact n2 location: 0x1bcd014
Contact nst location: 0x1bcd018
Information about the current block of memory:
Understanding how to do this will be useful when you implement malloc!
meta_data * head
: head for linked-listtotal_memory_requested
: keep track of bytes usedtotal_memory_freed
: keep track of bytes freedinvalid_addresses
char*