Sunday, August 8, 2010

zero sized allocation using malloc

int main()
{
int *p = 0;
printf("before addr: %pn", p);
p = (int *) malloc(0);
printf("after addr: %pn", p);
printf("sizeof: %un", sizeof(*p));
*p = 1;
printf("--- %d -- this is the last statment.n", *p);
free(p);
}

Output
before addr: (nil)
after addr: 0x80496c8
sizeof: 4
--- 1 -- this is the last statment.

Note :
Linux
  • allows a ‘read’ of the zero sized allocated memory
  • allows a ‘write’ on the zero sized allocated memory
  • sizeof shows an allocation of 4 bytes.

No comments:

Post a Comment