Differences between rel1 and rel3

Philip Ludlam philip at philipnet.com
Sun Sep 7 16:13:08 PDT 2003


On 7 Sep, in message <7c2b592e4c.jperry at hanover-square.freeserve.co.uk>
  Jason Perry <jperry at hanover-square.freeserve.co.uk> wrote:

>
>Hi there,
>
>I have a piece of code which works fine when built with GCC rel1 but
>fails when built with GCC rel3.
>
>The reason is because of the way structures are packed is handled
>differently between the two versions e.g.
>
>struct data_desc {
>  u_int8_t  Length;
>  u_int8_t  Type;
>  u_int16_t version;
>  u_int8_t  CountryCode;
>  u_int8_t  Number;
>};
>
>Under rel1, sizeof() reports six, but with rel3 it reports 20 presumably
>because it is now allocating 4 bytes per member in memory.
>
>I was wondering if there is a compiler switch or #pragma which would
>make rel3 behave like rel1 in this respect as this is causing having in
>a piece of code that otherwise works fine.

The C standard does not specify how compilers should layout in memory
structures. Assuming that sizeof <struct> doesn't change from compiler
to compiler and from version to version of the same compiler is likely
to result in undefined behaviour - as you have just found out.

I can't remember if the standard says that with:
  data_desc d[2];
that:
  &(d[1]) == &(d[0]) + sizeof data_desc;
(that the address of the 2nd element of an array == address of the 1st
element + size of the array type).
If it does, you might be able to use that, but you really ought to
check K&R2 first.

Yours,

Phil L.
-- 
http://www.philipnet.com                http://director.sourceforge.net
                i ou a uea i e a o ie e a o a a oue oae




More information about the gcc mailing list