Prev: C/C++ query
Next: output?
From: Sopel on
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.

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

From: CornedBee on
On Aug 6, 3:23 am, Abhishek <goluagarw...(a)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


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

From: Andrea Venturoli on
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.

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

From: Vaclav Haisman on
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

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




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

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