Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

C/C++ query

0 views
Skip to first unread message

Abhishek

unread,
Aug 6, 2010, 6:23:32 AM8/6/10
to
Initializing an array via a variable is restricted in C++.

int a=5;
int arr[a];//error

I checked it in GNU compilers to my astonishment that this code really
works.

Also suppose an array is declared to hold 5 data.i.e.

int arr[5];

Now, if you try to assign value to array element even greater than 5,
the compiler doesnt have a problem.

arr[7]=10;//this works


Kindly solve the doubt as to what is going wrong.

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

Message has been deleted

Ulrich Eckhardt

unread,
Aug 7, 2010, 6:18:39 AM8/7/10
to
Note up front: "C/C++ query" is not a good subject. All trafic here is
either meta-topical or concerns C++.

Abhishek wrote:
> Initializing an array via a variable is restricted in C++.
>
> int a=5;
> int arr[a];//error
>
> I checked it in GNU compilers to my astonishment that this code really
> works.

Right, that is a GNU extension. I believe that C99 also added something like
that.

> Also suppose an array is declared to hold 5 data.i.e.
>
> int arr[5];
>
> Now, if you try to assign value to array element even greater than 5,
> the compiler doesnt have a problem.
>
> arr[7]=10;//this works

No, it just doesn't cause a compiler error but it causes undefined behaviour
at runtime.

Uli

--
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

Ulrich Eckhardt

unread,
Aug 7, 2010, 6:19:21 AM8/7/10
to
Abhishek wrote:
> For a const variable, in general the compiler does'nt allocate memory
> for that variable in C++.

A constant can be easily propagated from the data section into the code, so
many compilers don't do that. This is by no means guaranteed or mandated
though.

> So you cannot reference the variable to initialize any array for its
> size as:
>
> const int a=5;
> int arr[a];//incorrect

Firstly, that code is correct. Secondly, your conclusion that you can't use
a constant for the size of an array because the constant is not actually
stored anywhere is something I can't follow.

> The above code is also verified to be incorrect in Bruce Eckel.

I doubt that.

red floyd

unread,
Aug 7, 2010, 7:18:36 AM8/7/10
to
On Aug 6, 3:48 am, Abhishek <goluagarw...@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.


--

CornedBee

unread,
Aug 7, 2010, 9:25:35 PM8/7/10
to
On Aug 6, 3:48 am, Abhishek <goluagarw...@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

This is perfectly valid code, and your explanation as to why it
shouldn't be is nonsensical. Integral const variables initialized with
an integer constant expression are in turn integer constant
expressions, and are thus valid in array bounds.

>
> The above code is also verified to be incorrect in Bruce Eckel.

I would love to see the quote for that.

Sebastian

Sopel

unread,
Aug 7, 2010, 9:26:15 PM8/7/10
to
According to the documentation:
http://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html,
GNU compiler has an extension for supporting variable-length arrays in
C and C++. Although this feature was introduced to ISO C99, it is not
portable.

CornedBee

unread,
Aug 7, 2010, 9:24:56 PM8/7/10
to
On Aug 6, 3:23 am, Abhishek <goluagarw...@gmail.com> wrote:
> Initializing an array via a variable is restricted in C++.
>
> int a=5;
> int arr[a];//error
>
> I checked it in GNU compilers to my astonishment that this code really
> works.

Did you use disable GCC's language extensions? GCC supports C99-style
variable-length arrays in C++ as an extension, which is why this code
is accepted.

>
> Also suppose an array is declared to hold 5 data.i.e.
>
> int arr[5];
>
> Now, if you try to assign value to array element even greater than 5,
> the compiler doesnt have a problem.

True, but the runtime behavior of your program is undefined. C++
implementations in general do not provide memory safety. It's your job
as a programmer to make sure you don't mess up.

Sebastian

Andrea Venturoli

unread,
Aug 7, 2010, 9:34:20 PM8/7/10
to
Il 08/06/10 12:23, Abhishek ha scritto:

> Initializing an array via a variable is restricted in C++.
>
> int a=5;
> int arr[a];//error
>
> I checked it in GNU compilers to my astonishment that this code really
> works.
>...

> Kindly solve the doubt as to what is going wrong.
>

From GCC's manual (chapter 5.14):

> Variable-length automatic arrays are allowed in ISO C99, and as an
> extension GCC accepts them in C89 mode and in C++. (However, GCC's
> implementation of variable-length arrays does not yet conform in detail
> to the ISO C99 standard.)

See for yourself all the rest :-)

bye
av.

Vaclav Haisman

unread,
Aug 7, 2010, 9:32:24 PM8/7/10
to
Abhishek wrote, On 6.8.2010 12:48:
> 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:
This does not make sense.

>
> const int a=5;
> int arr[a];//incorrect
This is IMHO correct in any context because 'a' is constant expression.

>
> 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.

GNU compiler is called GCC. Dev-CPP is using Mingw which is a port of GCC to
Windows.

--
VH

red floyd

unread,
Aug 7, 2010, 11:04:25 PM8/7/10
to
On 8/6/2010 3:48 AM, Abhishek 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.
>

a is a compile time constant, the construct is fine. Eckel is not the
ultimate authority (no offense, Bruce, if you're reading this). This
behaviour is explicitly allowed by the Standard. Alas, my copy is at
work, and I can't cite chapter and verse.

If a was not const, then the construct would be illegal.


--

Francis Glassborow

unread,
Aug 7, 2010, 11:07:40 PM8/7/10
to
red floyd wrote:
> On Aug 6, 3:48 am, Abhishek <goluagarw...@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.

Nick Maclaren

unread,
Aug 7, 2010, 11:05:42 PM8/7/10
to
In article <e1e71a7d-f744-42c6...@s9g2000yqd.googlegroups.com>,

red floyd <redf...@gmail.com> wrote:
> On Aug 6, 3:48 am, Abhishek <goluagarw...@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.

tni

unread,
Aug 7, 2010, 11:08:22 PM8/7/10
to
On 2010-08-07 13:18, red floyd wrote:
> On Aug 6, 3:48 am, Abhishek<goluagarw...@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.

red floyd

unread,
Aug 8, 2010, 5:42:52 PM8/8/10
to
On 8/7/2010 4:18 AM, red floyd wrote:
> On Aug 6, 3:48 am, Abhishek<goluagarw...@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.

0 new messages