avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[avr-gcc-list] Re: zero length array in nocommon


From: David Brown
Subject: [avr-gcc-list] Re: zero length array in nocommon
Date: Thu, 24 Sep 2009 10:51:49 +0200
User-agent: Thunderbird 2.0.0.22 (Windows/20090605)

Ruud Vlaming wrote:
On Wednesday 23 September 2009 21:14, Joerg Wunsch wrote:
Ruud Vlaming <address@hidden> wrote:

I noticed a strange behaviour when you place a zero length array in
'nocommon'.
No idea why the behaviour changes, but as a note of warning: a
zero-length array is not allowed by the C standard.
Well, mayby not in ISO C, but certainly in GNU C:
  http://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html
thus at least the gcc compiler should be able to handle it :-)

No idea what you are trying to achieve here though.
Although the link gives an example how this could be usefull,
i found a different application. I configure my projects using
preprop defines, and if a don't need an array internally, the size
calculation simply produces a zero. That way no space is taken, and it is much easier than having if-defs around all arrays to see
if they accedently have size zero. But it seems it does not work
when they are not in the common space. This 'quirk' of gcc does not seem to be specifically avr related but probably we are the only one's left to care about the spoilled byte.

Ruud.

As shown by the link you gave, zero-length arrays are only really valid when they are the last element in a struct (they can be very useful there).

The problem boils down to that C requires every declared object to have a unique address. Theoretically, the compiler/linker could do this by aliasing the zero-length array into the middle of something else that is otherwise not addressed by the program. But in practice, it is done by allocating at least 1 byte to zero-length data.

The same issue occurs with C++ classes with no non-static data, which can sometimes be useful.





reply via email to

[Prev in Thread] Current Thread [Next in Thread]