Help Required - compilation error.

Jonathan Wakely jwakely.gcc@gmail.com
Thu Aug 11 17:07:00 GMT 2011


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