c - memory allocation for structures -
can please explain me peculiar output:
#include <stdio.h> typedef struct node { int i; struct node *next; }node; main() { node *p,*q; printf(" %u ",sizeof(node)); // 16 p = (node *)malloc(sizeof ( node ) ) ; printf(" %p ",p); // 0x1cea010 q = (node *)malloc(sizeof ( node ) ) ; printf("\n %p ",q); // 0x1cea030 }
i have 64 bit processor. when size shown 16 byes, why 32 byte allocated node?? checked out 32- bit machine. addresses had separation of 8 bytes. no padding , stuff. difference of 4 bytes solely cause of padding issue of 64 bit machine??
two malloc
calls aren't going return consecutive memory areas. better way test be:
main() { node *p; printf(" %u ",sizeof(node)); p = (node*)malloc(2 * sizeof (node)); printf(" %p \n %p ", &p[0], &p[1]); free(p); }
by allocating array, can sure back-to-back in memory.
depending on implementation of malloc
, system may using memory in between p
, q
store bookkeeping information used realloc
, free
, , friends.
Comments
Post a Comment