defining big types on avr-gcc
Andrew Haley
aph@redhat.com
Tue Aug 3 16:03:00 GMT 2010
On 08/03/2010 04:26 PM, Massimiliano Cialdi wrote:
> I use avr-gcc 4.4.4:
>
> Using built-in specs.
> Target: avr
> Configured with: ../gcc-4.4.4/configure --target=avr
> --enable-languages=c,c++ --disable-nls --disable-libssp --with-dwarf2
> --prefix=/usr/local/cross-gcc-avr-4.4.4 --disable-threads --with-gnu-as
> --with-gnu-ld
> Thread model: single
> gcc version 4.4.4 (GCC)
>
>
>
> and my target is an ATMega32
>
> I tried to define a type (only define, without allocate any variable):
>
>
>
> typedef struct dummy
> {
> uint8_t data[9000];
> } dummy_t;
>
> typedef struct big_dummy
> {
> dummy_t bigdata[4];
> dummy_t singledata;
> } big_dummy_t;
>
>
>
> I never allocate any big_dummy_t object. I use big_dummy_t only as
> placeholder (I use offsetof() macro), only to calculate offsets of
> object placed into a big extern serial flash memory.
>
> I get the error from gcc:
>
> error: size of array 'bigdata' is too large
>
> it is 36000 bytes, bigger than 32KB.
>
> after many tests I found a possible solution:
>
> typedef struct dummy
> {
> uint8_t data[9000];
> } dummy_t;
>
> typedef struct big_dummy
> {
> union
> {
> dummy_t bigdata[0];
> struct
> {
> dummy_t bigdata0;
> dummy_t bigdata1;
> dummy_t bigdata2;
> dummy_t bigdata3;
> };
> };
> dummy_t singledata;
> } big_dummy_t;
>
> now if I want the offset of a field I can do as follows:
>
> uint16_t p = (uint16_t)&(((big_dummy_t*)0)->bigdata[3]);
>
> This works, but if I wand offset of singledata:
>
> uint16_t p = (uint16_t)&(((big_dummy_t*)0)->singledata);
>
> I get a warning: 'integer overflow in expression'
>
> but if I change in this way:
> big_dummy_t *p = (big_dummy_t*)0;
> uint16_t p = (uint16_t)&(p->singledata);
>
> I get NO warning!!!
>
> This looks incredible to me. I wonder why.
> Is it a bug?
I don't think it is. gcc uses ssize_t (or, to be precise, a signed
integer type beased on sizetype) internally when constructing the size
of an array. Given that array offsets can be negative or positive.
this seems reasonable enough.
Andrew.
More information about the Gcc-help
mailing list