Solaris : Difference in g++ and CC compiler for variable length array declaration

A

amjain.gzb

Greetings,

I am trying to compile some code on Solaris. I need to compile that
using CC instead of g++.

Following small program is representation of my problem

/home/ajain/warningRemoval>cat varsizearray.cpp

int main()
{
int len = 2*3 ;
int a[len];
a[1]=34;
}

----------------------

/home/ajain/warningRemoval>g++ varsizearray.cpp
/home/ajain/warningRemoval>CC varsizearray.cpp
"varsizearray.cpp", line 4: Error: An integer constant expression is
required within the array subscript operator.
1 Error(s) detected.
/home/ajain/warningRemoval>which CC
/opt/SUNWspro/bin/CC
/home/ajain/warningRemoval>

My doubt is whether some flag exists for CC which can help me remove
this error. I have such declarations at many places in my code so I do
not want to change code for this.

Thanks in advance,
regards,
Amit Jain
(e-mail address removed)
 
I

Ian Collins

Greetings,

I am trying to compile some code on Solaris. I need to compile that
using CC instead of g++.

Following small program is representation of my problem

/home/ajain/warningRemoval>cat varsizearray.cpp

int main()
{
int len = 2*3 ;
int a[len];

This isn't C++, it's gcc specific.

Use a vector.
 
I

Ian Collins

Ian said:
Greetings,

I am trying to compile some code on Solaris. I need to compile that
using CC instead of g++.

Following small program is representation of my problem

/home/ajain/warningRemoval>cat varsizearray.cpp

int main()
{
int len = 2*3 ;
int a[len];


This isn't C++, it's gcc specific.

Use a vector.
<OT>
I forgot to add that the current CC accepts this if use use const int
for the size.
</OT>
In future, use http://forum.sun.com/forum.jspa?forumID=5 for Sun CC
questions.
 
J

John Carson

Ian Collins said:
Ian said:
Greetings,

I am trying to compile some code on Solaris. I need to compile that
using CC instead of g++.

Following small program is representation of my problem

/home/ajain/warningRemoval>cat varsizearray.cpp

int main()
{
int len = 2*3 ;
int a[len];


This isn't C++, it's gcc specific.

Use a vector.
<OT>
I forgot to add that the current CC accepts this if use use const int
for the size.
</OT>

int main()
{
const int len = 2*3;
int a[len];
}

should be accepted by any standard compliant compiler. On the other hand,
the following is non-standard:

int MakeInt()
{
return 6;
}

int main()
{
const int len = MakeInt();
int a[len];
}

According to the standard, array size must be an "integral constant
expression" (section 8.3.4/1). This in turn is defined by section 5.19/1.

"An integral constant-expression can involve only literals (2.13),
enumerators, const variables or static data members of integral or
enumeration types initialized with constant expressions (8.5), non-type
template parameters of integral or enumeration types, and sizeof
expressions. Floating literals (2.13.3) can appear only if they are cast to
integral or enumeration types. Only type conversions to integral or
enumeration types can be used. In particular, except in sizeof expressions,
functions, class objects, pointers, or references shall not be used, and
assignment, increment, decrement, function-call, or comma operators shall
not be used."
 
A

amjain.gzb

Hi,
Thanks for all the responses but to start with my problem was that I
dont want to change the code because of number of changes in involves.
At the same time I was surprised by this problem since g++ compiler on
the same machine (Sun 5.8) was compiling this program and not CC (Sun
5.7 compiler).

I have found the solution to this.

I simply replaced
int array[len]; --------------------with--> int *array = (int*)
alloca(sizeof(int)*len) ;

---------------------------------------------------------------
/home/ajain/warningRemoval>cat varsizearray.cpp
#include <stdio.h>
#include <alloca.h>
int main()
{
int len = 2*3 ;
int i ;
#ifdef EXPAND_VLA
int *array = (int*) alloca(sizeof(int)*len) ;
#else
int array[len];
#endif

for(i=-1;++i<len;)array = i*2 ;
for(i=-1;++i<len;)printf("\n%d\t",array);

}
 
?

=?iso-8859-1?q?Stephan_Br=F6nnimann?=

John said:
int main()
{
const int len = 2*3;
int a[len];
}

should be accepted by any standard compliant compiler. On the other hand,
the following is non-standard:

int MakeInt()
{
return 6;
}

int main()
{
const int len = MakeInt();
int a[len];
}

According to the standard, array size must be an "integral constant
expression" (section 8.3.4/1). This in turn is defined by section 5.19/1.

"An integral constant-expression can involve only literals (2.13),
enumerators, const variables or static data members of integral or
enumeration types initialized with constant expressions (8.5), non-type
template parameters of integral or enumeration types, and sizeof
expressions. Floating literals (2.13.3) can appear only if they are cast to
integral or enumeration types. Only type conversions to integral or
enumeration types can be used. In particular, except in sizeof expressions,
functions, class objects, pointers, or references shall not be used, and
assignment, increment, decrement, function-call, or comma operators shall
not be used."

I do not agree with you, the definition "integral constant expression"
explicitly allows for "const variables ... of integral ... types"; and
(hopefully for me) you'll agree that `const int' fulfills this
criteria.

Regards, Stephan
 
I

Ian Collins

Hi,
Thanks for all the responses but to start with my problem was that I
dont want to change the code because of number of changes in involves.
At the same time I was surprised by this problem since g++ compiler on
the same machine (Sun 5.8) was compiling this program and not CC (Sun
5.7 compiler).

I have found the solution to this.

I simply replaced
int array[len]; --------------------with--> int *array = (int*)
alloca(sizeof(int)*len) ;
And that's easier than adding 'const'?
 
R

Ron Natalie

Stephan said:
I do not agree with you, the definition "integral constant expression"
explicitly allows for "const variables ... of integral ... types"; and
(hopefully for me) you'll agree that `const int' fulfills this
criteria.

The part that you omitted above with the ... says that const ints are
only constant expresssions when they are initialized with other constant
expressions.

Here's the full text:
 
?

=?iso-8859-1?q?Stephan_Br=F6nnimann?=

Ron said:
The part that you omitted above with the ... says that const ints are
only constant expresssions when they are initialized with other constant
expressions.

Here's the full text:

Yes you are right, I didn't read precisely enough.
Stephan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,227
Latest member
Daniella65

Latest Threads

Top