zero size objects

Playing with zero size objects is not always the same, or what you expect.

What’s the difference between the following length and pointer pairs?

size_t len1 = 0;
char *ptr1 = NULL;size_t len2 = 0;
char *ptr2 = malloc(0);size_t len3 = 0;
char *ptr3 = (char *)malloc(4096) + 4096;
size_t len4 = 0;
char ptr4[0];

size_t len5 = 0;
char ptr5[];

In many circumstances, all five result in identical behavior. Other times, the behavior can vary wildly. One obvious difference is whether the pointer can be passed to free, though I’m going to ignore that difference for the rest of this post.

The first case is interesting, but sufficiently different to postpone discussion for later.

Source: zero size objects

Leave a Reply

Your email address will not be published. Required fields are marked *