Topic: Sizeof expression legality
Author: eanderso@usa.net
Date: 1998/09/21 Raw View
In article <6t3tan$57k$1@mulga.cs.mu.OZ.AU>,
fjh@cs.mu.OZ.AU (Fergus Henderson) wrote:
> Edward Diener <eddielee@abraxis.com> writes:
>
> >Is the expression "sizeof( long [] )" legal in Ansi C++ as opposed to
> >"sizeof(long *)" which I know is legal ?
>
> No, `long []' is an incomplete type, and the type used in a sizeof()
> expression must be a complete type (see 5.3.3 paragraph 1).
Could you elaborate on this? I've got a similar problem (can't sizeof() a
perfectly good struct. What is this 5.3.3 paragraph 1?
-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp Create Your Own Free Member Forum
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: ncm@nospam.cantrip.org (Nathan Myers)
Date: 1998/09/21 Raw View
<eanderso@usa.net> wrote:
>
>In article <6t3tan$57k$1@mulga.cs.mu.OZ.AU>,
> fjh@cs.mu.OZ.AU (Fergus Henderson) wrote:
>> Edward Diener <eddielee@abraxis.com> writes:
>>
>> >Is the expression "sizeof( long [] )" legal in Ansi C++ as opposed to
>> >"sizeof(long *)" which I know is legal ?
>>
>> No, `long []' is an incomplete type, and the type used in a sizeof()
>> expression must be a complete type (see 5.3.3 paragraph 1).
>
>Could you elaborate on this? I've got a similar problem (can't sizeof() a
>perfectly good struct. What is this 5.3.3 paragraph 1?
-1- The sizeof operator yields the number of bytes in the object
representation of its operand. The operand is either an expression,
which is not evaluated, or a parenthesized type-id. The sizeof
operator shall not be applied to an expression that has function or
incomplete type, or to an enumeration type before all its enumerators
have been declared, or to the parenthesized name of such types, or to
an lvalue that designates a bit-field. sizeof(char), sizeof(signed
char) and sizeof(unsigned char) are 1; the result of sizeof applied to
any other fundamental type (basic.fundamental) is implementation-
defined.
A struct is "complete" only when its definition has been seen:
struct a; // incomplete
int b[sizeof(a)]; // error, a incomplete
struct a {}; // now complete
int c[sizeof(a)]; // OK.
--
Nathan Myers
ncm@nospam.cantrip.org http://www.cantrip.org/
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: fjh@cs.mu.OZ.AU (Fergus Henderson)
Date: 1998/09/08 Raw View
Edward Diener <eddielee@abraxis.com> writes:
>Is the expression "sizeof( long [] )" legal in Ansi C++ as opposed to
>"sizeof(long *)" which I know is legal ?
No, `long []' is an incomplete type, and the type used in a sizeof()
expression must be a complete type (see 5.3.3 paragraph 1).
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh> | of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3 | -- the last words of T. S. Garp.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Edward Diener <eddielee@abraxis.com>
Date: 1998/09/04 Raw View
Is the expression "sizeof( long [] )" legal in Ansi C++ as opposed to
"sizeof(long *)" which I know is legal ? The MIDL compiler in Windows
produces the former within code on same occasions given a valid IDL
construct .
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]