Can someone please explain me this 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 a 64 bit processor. When the size is shown to be 16 byes, why is 32 byte allocated for the node??
I checked out a 32- bit machine. The addresses had a separation of 8 bytes. With no padding and stuff. So is the difference of 4 bytes solely cause of some padding issue of the 64 bit machine??
Two
malloccalls aren’t necessarily going to return consecutive memory areas. A better way to do this test would be:By allocating an array, you can be sure that they are back-to-back in memory.
Depending on your implementation of
malloc, your system may be using the memory in betweenpandqto store bookkeeping information that is used byrealloc,free, and friends.