This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Buggish behaviour of nested unnamed struct in extern "C" clause?


Hi to all, my name's David Lucena, and I am new to the list. I have this file: test.c
----------------------------
#include <stdio.h>

struct tA
{
    int val1;
};

typedef struct tA A;

struct tB
{
    struct tA;
    int val2;
};

typedef struct tB B;

void Assign()
{
    A a;
    B b;
    
    a.val1 = 10;
    b.val1 = 20;
    b.val2 = 30;
    
    
    printf( "sizeof(A) %d\n", sizeof(A) );
    printf( "sizeof(B) %d\n", sizeof(B) );
}

int main( int argc, char* argv[] )
{
    A a;
    B b;
    
    a.val1 = 10;
    b.val1 = 20;
    b.val2 = 30;

    printf( "sizeof(A) %d\n", sizeof(A) );
    printf( "sizeof(B) %d\n", sizeof(B) );

    Assign();

    return 0;
}
----------------------------

I compile it with gcc and -fms-extensions, and works like a charm. sizeof(A) is 4, sizeof(B) is 8,
and b.val1 assignment is ok.

I have another program: main.cpp
----------------------------
#include <stdio.h>

extern "C"
{
    struct tA
    {
        int val1;
    };

    typedef struct tA A;

    struct tB
    {
        struct tA;
        int val2;
    };

    typedef struct tB B;

    void Assign()
    {
        A a;
        B b;
        
        a.val1 = 10;
        b.val1 = 20;
        b.val2 = 30;
        
        
        printf( "sizeof(A) %d\n", sizeof(A) );
        printf( "sizeof(B) %d\n", sizeof(B) );
    }
}

int main( int argc, char* argv[] )
{
    A a;
    B b;
    
    a.val1 = 10;
    b.val1 = 20;
    b.val2 = 30;
    
    printf( "sizeof(A) %d\n", sizeof(A) );
    printf( "sizeof(B) %d\n", sizeof(B) );

    Assign();

    return 0;
}
----------------------------
I compile it with g++. It fails on assigning b.val1, both in Assign() and in main. If I comment
the assignments, it compiles, but sizeof(A) is 4, and sizeof(B) is also 4.

What I am missing? I need to use structures like thesse in C++ code, instead of using C++ classes
because I have some already defined from another project for an embedded system that is C pure,
from which I want to make some tools in PC for faster processing.

Thank you very much in advance.


       
______________________________________________ 
¿Chef por primera vez?
Sé un mejor Cocinillas. 
http://es.answers.yahoo.com/info/welcome


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]