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

Please Help.

Regards,
Abhishek

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

From: Ulrich Eckhardt on
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


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

From: Ulrich Eckhardt on
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.

Uli

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


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


--
[ 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: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

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


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

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