Topic: sizeof ( struct defined in class ) == 0


Author: Steve Clamage <stephen.clamage@sun.com>
Date: 1999/11/27
Raw View
by wrote:
>
> I got this surprising behavior in Visual C++:
>
> class C {
>     ...
>     struct S { /*structure S is not empty*/
>         void*p;
>         ...
>     }
> }
>
> void C:Function()
> {
>      i=sizeof(S); /*now i is zero*/
>      i=sizeof(C::S); /*now i is still zero*/
>      i=sizeof(struct C::S); /*now i has the value I expected*/
> }

First of all, there are no zero-sized complete types, so sizeof
cannot yield zero in standard C++. But a compiler might implement
a non-standard extension that allowed taking sizeof an incomplete
type or object, returning zero. I don't know whether that is the
case in VC++. What happens in this example?
 #include <stddef.h>
 class A;
 size_t a = sizeof(A); // should be error, A incomplete
 extern int B[];
 size_t b = sizeof(B); // should be error, int[] incomplete

Is there something else in class C or in C::Function (I'm assuming
C:Function is a typo) named S? You are allowed to have an object
with the same name as a class, in which case you can refer to the
class name only with the keyword "struct" or "class".

Without seeing the rest of class C, it's hard to say what's going
on.  It would help to provide a compilable example that illustrates
the problem.

--
Steve Clamage, stephen.clamage@sun.com


[ 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: "by" <ecxz@yahoo.com>
Date: 1999/11/27
Raw View
I got this surprising behavior in Visual C++:

class C {
    ...
    struct S { /*structure S is not empty*/
        void*p;
        ...
    }
}

void C:Function()
{
     i=sizeof(S); /*now i is zero*/
     i=sizeof(C::S); /*now i is still zero*/
     i=sizeof(struct C::S); /*now i has the value I expected*/
}

If I declare S as a global structure outside the class C definition,
sizeof(S) works. What's going on ? Is this some obscure C++ rule or Visual C
has a bug ?

Thanks, Bo
---
[ 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              ]