Re: Help Required - compilation error.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi All,

Thanks for your responses.

 offsetof(DATASTRUCTURE, mydata) worked for me.

Regards
Jalaiah. M


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

On 11 August 2011 17:25,  <david.hagood@xxxxxxxxx> 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)];




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux