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