Auto variable with sizeof == 0

escott%deis.uci.edu at icsg.uci.edu escott%deis.uci.edu at icsg.uci.edu
Fri Jan 30 14:37:52 AEST 1987


Somebody recently came to me with a program that worked on a VAX 11/750
running 4.2BSD but failed on our Sequent Balance 21000 running Dynix 2.1.
The apparent culprit was of course the C compiler on the latter machine.
However, after examining the code in question, I found a construct that
seems a little strange to me: an automatic variable was declared as a
"struct foo **bar[]".  "How could this be right?"  I said to myself.  "How
can you declare an automatic variable that has no size?"

So I wrote a program that contained a similar declaration, and then tried to
take sizeof( bar ).  Sure enough:

	warning: sizeof returns 0

[This from the VAX 11/750 4.2BSD compiler]

Okay, that makes sense.  My question is: is there any reason why you should
be able to declare an array with zero elements as an automatic variable?
What's strange is that, on the VAX, the program apparently successfully
dereferenced bar, both setting a value for "*bar" and then using that value
later.  How can this be right?  How can "bar" have any value at all, much
less "*bar"?  If there is no use for a zero-sized automatic variable, how
come the compiler lets you do it?  (Even a C compiler should occasionally
clamp down 8^).  And, just for the heck of asking, does ANSI C let you make
such a declaration?

+-------------------------------------------------------------------------+
 Scott Menter  UCI ICS Computing Support Group   Univ. of Calif. at Irvine
                     (714) 856 7552              Irvine, California  92717

 Internet:  escott at ics.uci.edu             UUCP:  ...!ucbvax!ucivax!escott
 Bitnet:    escott at uci               CSNet: escott%ics.uci.edu at csnet-relay
 Internet (with Name Server):  TBA
+-------------------------------------------------------------------------+



More information about the Comp.lang.c mailing list