Help Required - compilation error.

Jalaiah Murari m_jalaiah@yahoo.com
Fri Aug 12 06:15:00 GMT 2011


Hi All,

Thanks for your responses.

 offsetof(DATASTRUCTURE, mydata) worked for me.

Regards
Jalaiah. M


----- Original Message -----
From: Jonathan Wakely <jwakely.gcc@gmail.com>
To: david.hagood@gmail.com
Cc: Jalaiah Murari <m_jalaiah@yahoo.com>; "gcc-help@gcc.gnu.org" <gcc-help@gcc.gnu.org>
Sent: Thursday, August 11, 2011 10:37 PM
Subject: Re: Help Required - compilation error.

On 11 August 2011 17:25,  <david.hagood@gmail.com> wrote:
>> Hi,
>>
>> I used GCC 2.7.2.3 in the past to compile the following code:
>> class sample
>> {
>> private:
>> unsigned char wk_tmp[20000 - ((int)(long)&(((DATASTRUCTURE*)0)->mydata))];
>>
>> };
>
> Your code is broken.
>
> You REALLY want to use a smarter type here:
>
> #include <vector>
> class sample
> {
>   private:
>     std::vector<unsigned char> wk_temp;
>   public:
>     sample();
> };
>
> sample::sample()
> :wk_temp((int)(long)&(((DATASTRUCTURE*)0)->mydata)))

That will dereference a null pointer at runtime, probably not what's intended!

I agree the code is broken, but as it appears to be an attempt to find
the address of &DATASTRUCTURE::mydata relative to a DATASTRUCTURE
object at address 0, I think it's equivalent to
offsetof(DATASTRUCTURE, mydata), which is a constant, so the original
declaration can be replaced with:

unsigned char wk_tmp[20000 - offsetof(DATASTRUCTURE, mydata)];



More information about the Gcc-help mailing list