* Help Required - compilation error.
@ 2011-08-11 13:51 Jalaiah Murari
2011-08-11 14:06 ` Jonathan Wakely
2011-08-11 16:26 ` david.hagood
0 siblings, 2 replies; 5+ messages in thread
From: Jalaiah Murari @ 2011-08-11 13:51 UTC (permalink / raw)
To: gcc-help
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))];
};
Now, When i try to compile using GCC 4.1.2, I get the following error for the above declaration.
error: array bound is not an integer constant
I see that the array size has to be mentioned directly, instead of dynamic calculation.
Is there any workaround for this. Do I need to turn on any flag in gcc/g++ when compiling this file.
Thanks in advance.
Regards
Jalaiah. M
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Help Required - compilation error.
2011-08-11 13:51 Help Required - compilation error Jalaiah Murari
@ 2011-08-11 14:06 ` Jonathan Wakely
2011-08-11 16:26 ` david.hagood
1 sibling, 0 replies; 5+ messages in thread
From: Jonathan Wakely @ 2011-08-11 14:06 UTC (permalink / raw)
To: Jalaiah Murari; +Cc: gcc-help
On 11 August 2011 14:50, Jalaiah Murari 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))];
>
> };
>
>
> Now, When i try to compile using GCC 4.1.2, I get the following error for the above declaration.
> error: array bound is not an integer constant
>
>
> I see that the array size has to be mentioned directly, instead of dynamic calculation.
> Is there any workaround for this. Do I need to turn on any flag in gcc/g++ when compiling this file.
No, the code is invalid and can't be compiled.
It looks as though you probably want to use offsetof(DATASTRUCTURE, mydata)
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Help Required - compilation error.
2011-08-11 13:51 Help Required - compilation error Jalaiah Murari
2011-08-11 14:06 ` Jonathan Wakely
@ 2011-08-11 16:26 ` david.hagood
2011-08-11 17:07 ` Jonathan Wakely
1 sibling, 1 reply; 5+ messages in thread
From: david.hagood @ 2011-08-11 16:26 UTC (permalink / raw)
To: Jalaiah Murari; +Cc: gcc-help
> 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)))
{
}
Using a vector will let the language handle the allocation of the space,
check for overflows, and so on.
(now, the wisdom of what you are using this for - why the allocation type,
etc. are another issue you may want to think about, but you didn't give us
enough information to help you there.)
Bottom line: you REALLY shouldn't be using dumb types like C arrays or
pointers in C++ if you can avoid it.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Help Required - compilation error.
2011-08-11 16:26 ` david.hagood
@ 2011-08-11 17:07 ` Jonathan Wakely
2011-08-12 6:15 ` Jalaiah Murari
0 siblings, 1 reply; 5+ messages in thread
From: Jonathan Wakely @ 2011-08-11 17:07 UTC (permalink / raw)
To: david.hagood; +Cc: Jalaiah Murari, gcc-help
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)];
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Help Required - compilation error.
2011-08-11 17:07 ` Jonathan Wakely
@ 2011-08-12 6:15 ` Jalaiah Murari
0 siblings, 0 replies; 5+ messages in thread
From: Jalaiah Murari @ 2011-08-12 6:15 UTC (permalink / raw)
To: Jonathan Wakely, david.hagood; +Cc: gcc-help
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)];
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2011-08-12 6:15 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-08-11 13:51 Help Required - compilation error Jalaiah Murari
2011-08-11 14:06 ` Jonathan Wakely
2011-08-11 16:26 ` david.hagood
2011-08-11 17:07 ` Jonathan Wakely
2011-08-12 6:15 ` Jalaiah Murari
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).