Question about -ansi flag, and different size of struct from C and C++

Ralf Corsepius corsepiu at faw.uni-ulm.de
Mon Dec 15 12:36:26 UTC 2003


On Mon, 2003-12-15 at 13:02, Oleg Ivanov wrote:
>     Hello list,
>     I have a problem. I defined struct like:
>         struct dummy {
>           int var1;
>           short var2;
>           int var3;
>        } __attribute__ ((packed));
> 
>     and access to it from C and C++ code.
>     The problem is that size from C is 10 bytes and from C++ is 12 
> bytes, I couldn't make that C++ packed that struct, exept removing -ansi 
> flag from compiler string.
>     So I have two question, is there another method to ask C++ packed 
> struct, 
__attribute__ is a gcc extension, 
#pragma Packed rsp. __attribute__((packed)) are compiler-dependent
features,
"packing" in general is target dependent.

I.e. this is dirty, non-portable and target-dependent code.

In one sentence: Don't do that! ;)

I don't know why gcc accepts __attribute__((packed)) and g++ doesn't.
IMO, gcc and g++ should both either refuse to take this code if using
-ansi or silently not pack.

> and do we really need -ansi flag to compile rtems ?
Well, the -ansi is there to prevent people from writing dirty code and
to enforce ANSI-compliance. Otherwise, people tend to be careless about
adopting functions they have seen somewhere.

Ralf





More information about the users mailing list