News:

This week IPhone 15 Pro winner is karn
You can be too a winner! Become the top poster of the week and win valuable prizes.  More details are You are not allowed to view links. Register or Login 

Main Menu

c++ arrays tips - Size of zero element arrays

Started by ben2ong2, October 06, 2006, 05:56:29 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

ben2ong2

PROBLEM: You are not allowed to view links. Register or Login (Virendra Kumar Mehta)

Just out of curiousity, what is the sizeof() a zero length array?

A couple quick test programs I wrote tells me that

     typedef struct {char i[0];} ZZZ;
     main() {printf("%d\n",sizeof(ZZZ));}

the above had a size of 1 if I compile with a .cc extention (c++);
but size of 0 if I compile with a .c extention (c).


RESPONSE: You are not allowed to view links. Register or Login (Jim Adcock), Microsoft Corporation, 1 Mar 94

ARM says you cannot statically define arrays of #elements == 0, however
you are allowed to dynamically allocate arrays of #elements == 0, so
the static constraint makes no sense, IMHO.  In any case, the sizeof
an array = #elements * sizeof(element) "by definition" since the
sizeof(anything) is "by definition" the number of bytes that thing
takes *as an element in an array*.

Thus the sizeof() a zero length array by definition must be zero.
This is not the same as saying the number of bytes taken by a zero
length dynamic allocation is zero.  On the contrary, it is very common
for such to take on the order of 10 bytes of storage space "wasted".
You are not allowed to view links. Register or Login
You are not allowed to view links. Register or Login