Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > "error C2057: expected constant expression", "error C2466: cannot allocate an array of constant size 0". Why doesn't my simple program work???

Reply
Thread Tools

"error C2057: expected constant expression", "error C2466: cannot allocate an array of constant size 0". Why doesn't my simple program work???

 
 
hn.ft.pris@gmail.com
Guest
Posts: n/a
 
      01-20-2007
Hi:
I have the following simple program:

#include<iostream>
using namespace std;
int main(int argc, char* argv[]){

const double L = 1.234;
const int T = static_cast<const int>(L);
int arr[T];
return 0;
}

But I get the error message shown in title. Why doesn't my program
work? Thanks for help!

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      01-20-2007
On 2007-01-20 17:08, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi:
> I have the following simple program:
>
> #include<iostream>
> using namespace std;
> int main(int argc, char* argv[]){
>
> const double L = 1.234;
> const int T = static_cast<const int>(L);
> int arr[T];
> return 0;
> }
>
> But I get the error message shown in title. Why doesn't my program
> work? Thanks for help!


Because T is not constant at compile-time, which it needs to be for the
compiler to know how much space to allocate on the stack. That, of
course, is not the correct explanation according to the standard but it
quite nicely describes what is going on. Consider the following code in
which T is also constant but can wary with every run of the application:

#include<iostream>

int main()
{
double L;
std::cin >> L;
const int T = static_cast<const int>(L);
return 0;
}

--
Erik Wikström
 
Reply With Quote
 
 
 
 
Daniel T.
Guest
Posts: n/a
 
      01-20-2007
(E-Mail Removed) wrote:

> Hi:
> I have the following simple program:
>
> #include<iostream>
> using namespace std;
> int main(int argc, char* argv[]) {
> const double L = 1.234;
> const int T = static_cast<const int>(L);
> int arr[T];
> return 0;
> }
>
> Why doesn't my program work?


Good question. The code compiled fine for my g++ compiler, but not for
http://www.comeaucomputing.com/tryitout I have some questions about
comeau's output:

> "ComeauTest.c", line 6: warning: type qualifier is meaningless on cast type
> const int T = static_cast<const int>(L);
> ^


What is this? Isn't the 'const' required in the above context?

> "ComeauTest.c", line 7: error: constant value is not known
> int arr[T];
> ^


Why is that?
 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      01-20-2007
Erik Wikström wrote:

> On 2007-01-20 17:08, (E-Mail Removed) wrote:
>> Hi:
>> I have the following simple program:
>>
>> #include<iostream>
>> using namespace std;
>> int main(int argc, char* argv[]){
>>
>> const double L = 1.234;
>> const int T = static_cast<const int>(L);
>> int arr[T];
>> return 0;
>> }
>>
>> But I get the error message shown in title. Why doesn't my program
>> work? Thanks for help!

>
> Because T is not constant at compile-time,


Huh? How is that?

> which it needs to be for the
> compiler to know how much space to allocate on the stack. That, of
> course, is not the correct explanation according to the standard but it
> quite nicely describes what is going on. Consider the following code in
> which T is also constant but can wary with every run of the application:
>
> #include<iostream>
>
> int main()
> {
> double L;


You are cheating in the line above. The OP had:

const double L = 1.234;
^^^^^

> std::cin >> L;


With the line of the OP, this would be undefined behavior.

> const int T = static_cast<const int>(L);
> return 0;
> }



Best

Kai-Uwe Bux

 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      01-20-2007
Daniel T. wrote:
> (E-Mail Removed) wrote:
>
>> Hi:
>> I have the following simple program:
>>
>> #include<iostream>
>> using namespace std;
>> int main(int argc, char* argv[]) {
>> const double L = 1.234;
>> const int T = static_cast<const int>(L);
>> int arr[T];
>> return 0;
>> }
>>
>> Why doesn't my program work?

>
> Good question. The code compiled fine for my g++ compiler, but not for
> http://www.comeaucomputing.com/tryitout I have some questions about
> comeau's output:
>
>> "ComeauTest.c", line 6: warning: type qualifier is meaningless on cast type
>> const int T = static_cast<const int>(L);
>> ^

>
> What is this? Isn't the 'const' required in the above context?
>
>> "ComeauTest.c", line 7: error: constant value is not known
>> int arr[T];
>> ^

>
> Why is that?


In order for T to be a compile-time constant, it has to have an
initializer that's a compile-time constant. Floating-point values aren't.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      01-20-2007
Pete Becker <(E-Mail Removed)> wrote:
> Daniel T. wrote:
> > (E-Mail Removed) wrote:
> >
> > > Hi:
> > > I have the following simple program:
> > >
> > > #include<iostream>
> > > using namespace std;
> > > int main(int argc, char* argv[]) {
> > > const double L = 1.234;
> > > const int T = static_cast<const int>(L);
> > > int arr[T];
> > > return 0;
> > > }
> > >
> > > Why doesn't my program work?

> >
> > Good question. The code compiled fine for my g++ compiler, but not for
> > http://www.comeaucomputing.com/tryitout I have some questions about
> > comeau's output:
> >
> > > "ComeauTest.c", line 6: warning: type qualifier is meaningless on cast type
> > > const int T = static_cast<const int>(L);
> > > ^

> >
> > What is this? Isn't the 'const' required in the above context?


What about this warning?

> > > "ComeauTest.c", line 7: error: constant value is not known
> > > int arr[T];
> > > ^

> >
> > Why is that?

>
> In order for T to be a compile-time constant, it has to have an
> initializer that's a compile-time constant. Floating-point values aren't.


I didn't know that. Thanks.
 
Reply With Quote
 
hn.ft.pris@gmail.com
Guest
Posts: n/a
 
      01-21-2007

"Erik Wikström 写道:
"
> On 2007-01-20 17:08, (E-Mail Removed) wrote:
> > Hi:
> > I have the following simple program:
> >
> > #include<iostream>
> > using namespace std;
> > int main(int argc, char* argv[]){
> >
> > const double L = 1.234;
> > const int T = static_cast<const int>(L);
> > int arr[T];
> > return 0;
> > }
> >
> > But I get the error message shown in title. Why doesn't my program
> > work? Thanks for help!

>
> Because T is not constant at compile-time, which it needs to be for the
> compiler to know how much space to allocate on the stack. That, of
> course, is not the correct explanation according to the standard but it
> quite nicely describes what is going on. Consider the following code in
> which T is also constant but can wary with every run of the application:
>
> #include<iostream>
>
> int main()
> {
> double L;
> std::cin >> L;
> const int T = static_cast<const int>(L);
> return 0;
> }
>
> --
> Erik Wikström


Thanks for help, and here I've made some modification to the program.

#include<iostream>

int main()
{
#define L 1.234
#define T static_cast<const int>(L)
int arr[T];
return 0;
}

Does it mean that now T is a "compile-time determined" variable?
If it does, then could I suppose that "#define" is expanded and
calculated at complie-time?
Does all "#define" perform in a same way?
Thanks a lot for helping me.

 
Reply With Quote
 
Clark S. Cox III
Guest
Posts: n/a
 
      01-21-2007
Daniel T. wrote:
> Pete Becker <(E-Mail Removed)> wrote:
>> Daniel T. wrote:
>>> (E-Mail Removed) wrote:
>>>
>>>> Hi:
>>>> I have the following simple program:
>>>>
>>>> #include<iostream>
>>>> using namespace std;
>>>> int main(int argc, char* argv[]) {
>>>> const double L = 1.234;
>>>> const int T = static_cast<const int>(L);
>>>> int arr[T];
>>>> return 0;
>>>> }
>>>>
>>>> Why doesn't my program work?
>>> Good question. The code compiled fine for my g++ compiler, but not for
>>> http://www.comeaucomputing.com/tryitout I have some questions about
>>> comeau's output:
>>>
>>>> "ComeauTest.c", line 6: warning: type qualifier is meaningless on cast type
>>>> const int T = static_cast<const int>(L);
>>>> ^
>>> What is this? Isn't the 'const' required in the above context?

>
> What about this warning?


For built-in types, there is no difference between a const rvalue and a
non-const rvalue. That means that there is no difference between the
following casts (they both produce an rvalue of type int):

static_cast<int>(1.234)
static_cast<const int>(1.234)

So the const is meaningless in this context.


--
Clark S. Cox III
(E-Mail Removed)
 
Reply With Quote
 
=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      01-22-2007
On Jan 20, 8:10 pm, Kai-Uwe Bux <(E-Mail Removed)> wrote:
> Erik Wikström wrote:
> > On 2007-01-20 17:08, (E-Mail Removed) wrote:
> >> Hi:
> >> I have the following simple program:

>
> >> #include<iostream>
> >> using namespace std;
> >> int main(int argc, char* argv[]){

>
> >> const double L = 1.234;
> >> const int T = static_cast<const int>(L);
> >> int arr[T];
> >> return 0;
> >> }

>
> >> But I get the error message shown in title. Why doesn't my program
> >> work? Thanks for help!

>
> > Because T is not constant at compile-time,

>
> Huh? How is that?


Because the program have to execute the static_cast before the value of
T can be determined. And to declare an array on the stack you need to
know the size of the array at compile-time. Perhaps I was a bit
unclear, what I meant was that T was not a constant value at
compile-time.

> > which it needs to be for the
> > compiler to know how much space to allocate on the stack. That, of
> > course, is not the correct explanation according to the standard but it
> > quite nicely describes what is going on. Consider the following code in
> > which T is also constant but can wary with every run of the application:

>
> > #include<iostream>

>
> > int main()
> > {
> > double L;

>
> You are cheating in the line above. The OP had:
>
> const double L = 1.234;
> ^^^^^


Yes, but it's the value (and constness) of T that is of interest in
this problem.

--
Erik Wikström

 
Reply With Quote
 
=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      01-22-2007
On Jan 21, 10:51 am, (E-Mail Removed) wrote:
> Thanks for help, and here I've made some modification to the program.
>
> #include<iostream>
>
> int main()
> {
> #define L 1.234
> #define T static_cast<const int>(L)
> int arr[T];
> return 0;
>
> }
>
> Does it mean that now T is a "compile-time determined" variable?


No, what you have done is to use macros to restructure your code a bit,
after this has been through the pre-processor this is what you'll get:

#include<iostream>

int main()
{
int arr[static_cast<const int>(1.234)];
return 0;
}

> If it does, then could I suppose that "#define" is expanded and
> calculated at compile-time?


They are expanded, yes, but not calculated. Or rather the static_cast
is not evaluated until runtime, which is to late since the size of the
array needs to be known at compile-time.

> Does all "#define" perform in a same way?


All defines are expanded by the pre-processor, but that is all, if you
want code evaluated at compile-time you can do some things with
templates, but I don't think that's what you want.

What you can do is to allocate the array on the heap:

int main()
{
int* arr = new int[static_cast<const int>(1.234)];
delete[] arr; // don't forget this when you are done
return 0;
}

--
Erik Wikström

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dynamically allocate array size using malloc SAUHING LEE C Programming 7 12-06-2008 02:16 PM
Cannot allocate array saneman C++ 2 08-02-2008 08:18 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Dinamically allocate array of array of structures valerio C Programming 3 09-01-2006 11:45 PM



Advertisments