Prev: C/C++ query
Next: output?
From: Francis Glassborow on
red floyd wrote:
> On Aug 6, 3:48 am, Abhishek <goluagarw...(a)gmail.com> wrote:
>> For a const variable, in general the compiler does'nt allocate memory
>> for that variable in C++. So you cannot reference the variable to
>> initialize any array for its size as:
>>
>> const int a=5;
>> int arr[a];//incorrect
>>
>> The above code is also verified to be incorrect in Bruce Eckel. But to
>> my surprise its running well and fine in both GNU compiler and DEV-CPP
>> compiler.
> It's not surprising that DEV-CPP accepts it, since DEV-CPP uses g++ as
> its compiler.
> The issue is that it's a C99-ism (Variable Length Arrays), that g++
> accepts
> in C++ as an extension. Try using --std=c++98 and I suspect that the
> extension
> will not be accepted.


Please read the code carefully. That 'const' before the 'int' makes all the difference (in C++ though not in C). Indeed without it you would be in C99 variable length array territory but with it you are solidly in C++ where const makes it a compile time constant and so usable as an array size.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Nick Maclaren on
In article <e1e71a7d-f744-42c6-8b38-ef40a75c3cc8(a)s9g2000yqd.googlegroups.com>,
red floyd <redfloyd(a)gmail.com> wrote:
> On Aug 6, 3:48 am, Abhishek <goluagarw...(a)gmail.com> wrote:
>> For a const variable, in general the compiler does'nt allocate memory
>> for that variable in C++. So you cannot reference the variable to
>> initialize any array for its size as:
>>
>> const int a=5;
>> int arr[a];//incorrect
>>
>> The above code is also verified to be incorrect in Bruce Eckel. But to
>> my surprise its running well and fine in both GNU compiler and DEV-CPP
>> compiler.
>
> It's not surprising that DEV-CPP accepts it, since DEV-CPP uses g++ as
> its compiler.
>
> The issue is that it's a C99-ism (Variable Length Arrays), that g++
> accepts in C++ as an extension. Try using --std=c++98 and I suspect
> that the extension will not be accepted.

With one slight quibble, yes.

The variable length arrays that gcc supports predate C99 and are
not quite the same, unless that has been changed and I haven't
noticed. If you use them straightforwardly, you will not notice
any difference, but there are some subtle differences.


Regards,
Nick Maclaren.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: tni on
On 2010-08-07 13:18, red floyd wrote:
> On Aug 6, 3:48 am, Abhishek<goluagarw...(a)gmail.com> wrote:
>> For a const variable, in general the compiler does'nt allocate memory
>> for that variable in C++. So you cannot reference the variable to
>> initialize any array for its size as:
>>
>> const int a=5;
>> int arr[a];//incorrect
>>
>> The above code is also verified to be incorrect in Bruce Eckel. But to
>> my surprise its running well and fine in both GNU compiler and DEV-CPP
>> compiler.
>
> It's not surprising that DEV-CPP accepts it, since DEV-CPP uses g++ as
> its compiler.
>
> The issue is that it's a C99-ism (Variable Length Arrays), that g++
> accepts
> in C++ as an extension. Try using --std=c++98 and I suspect that the
> extension
> will not be accepted.

That's nonsense.

"const int a=5;" is an Integral Constant Expression and perfectly fine to use for declaring an array.

Take a look at the C++ standard, Section 5.19.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: red floyd on
On 8/7/2010 4:18 AM, red floyd wrote:
> On Aug 6, 3:48 am, Abhishek<goluagarw...(a)gmail.com> wrote:
>>
>> const int a=5;
>> int arr[a];//incorrect
[redacted]
> The issue is that it's a C99-ism (Variable Length Arrays), that g++
> accepts
> in C++ as an extension. Try using --std=c++98 and I suspect that the
> extension
> will not be accepted.
>

Oops, I thought I was replying to his other question (sans const).
OP really needs to learn how to write a subject line.

Of course the original code snippet is correct.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

First  |  Prev  | 
Pages: 1 2 3
Prev: C/C++ query
Next: output?