[GCC-XML] pragmas, preprocessor

Bjorn.Roald bjorn.roald at broadpark.no
Wed Sep 29 16:37:22 EDT 2004


On Thu, 2004-09-23 at 19:52 +0200, Thomas Heller wrote:
> Brad King <brad.king at kitware.com> writes:
> 
...
> This program compiles and works (1300 structures, more than 8000
> asserts), apart from one problem.  This structure gives the incorrect
> size (4 instead of 8):
> 
> struct {
>     int a;
>     struct U {
>         int b;
>     };
> } X;
> 
> Changing the definition to either this
> 
> struct {
>     int a;
>     struct U {
>         int b;
>     } u;
> } X;
> 
> or this
> 
> struct {
>     int a;
>     struct {
>         int b;
>     };
> } X;
> 
> gives the correct results.  I'm not sure the first definition is
> valid C - but it is in MS header files (objidl.h, struct _userSTGMEDIUM).
> 

Thomas, I just read your posting and it occurred to me that 4 bytes
might in fact be the correct answer.  I wrote a small test program and
compiled it with g++ (GCC) 3.4.1 20040831 (Red Hat 3.4.1-10)

#include <iostream>
using namespace std;

struct {
    int a;
    struct U {
        int b;
    };
} X;


struct {
    int a;
    struct U {
        int b;
    } u;
} Y;


struct {
    int a;
    struct {
        int b;
    };
} Z;



int main() {
    cout << "X=" << sizeof( X ) << endl
         << "Y=" << sizeof( Y ) << endl
         << "Z=" << sizeof( Z ) << endl;

    return 0;
}

prints:

X=4
Y=8
Z=8

I believe the reason is that struct variable named X only defines a new
type typeof(X)::U, there is no variable of that type in the struct
variable named X. The struct of variable X does not have a type name.

Struct variable named Y has a variable of type typeof(Y)::U named u, and
the struct variable named Z has an int variable named b in an unnamed
struct,... which I think can be accessed with something like;

Z.b = 5;

Some of the stuff in these examples are rather strange, but I think it
is legal.  I have used eg. typeof(X)::U above since the variable X does
not have a type name, I do not know a legal way of writing the type name
of the inner struct so I used the psuedo code.


Bjorn Roald




More information about the gccxml mailing list