Connecting Tech Pros Worldwide Help | Site Map

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

amjain.gzb@gmail.com
Guest
 
Posts: n/a
#1: Jan 25 '06
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
amjain.gzb@gmail.com

Ian Collins
Guest
 
Posts: n/a
#2: Jan 25 '06

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


amjain.gzb@gmail.com wrote:[color=blue]
> 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];[/color]

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

Use a vector.

--
Ian Collins.
Ian Collins
Guest
 
Posts: n/a
#3: Jan 25 '06

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


Ian Collins wrote:[color=blue]
> amjain.gzb@gmail.com wrote:
>[color=green]
>> 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];[/color]
>
>
> This isn't C++, it's gcc specific.
>
> Use a vector.
>[/color]
<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.

--
Ian Collins.
John Carson
Guest
 
Posts: n/a
#4: Jan 25 '06

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


"Ian Collins" <ian-news@hotmail.com> wrote in message
news:1138181286.151405@drone2-svc-skyt.qsi.net.nz[color=blue]
> Ian Collins wrote:[color=green]
>> amjain.gzb@gmail.com wrote:
>>[color=darkred]
>>> 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];[/color]
>>
>>
>> This isn't C++, it's gcc specific.
>>
>> Use a vector.
>>[/color]
> <OT>
> I forgot to add that the current CC accepts this if use use const int
> for the size.
> </OT>[/color]

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

--
John Carson


amjain.gzb@gmail.com
Guest
 
Posts: n/a
#5: Jan 25 '06

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


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] = i*2 ;
for(i=-1;++i<len;)printf("\n%d\t",array[i]);

}

Stephan Brönnimann
Guest
 
Posts: n/a
#6: Jan 25 '06

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


John Carson wrote:[color=blue]
> 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."[/color]

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

Ian Collins
Guest
 
Posts: n/a
#7: Jan 25 '06

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


amjain.gzb@gmail.com wrote:[color=blue]
> 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) ;
>[/color]
And that's easier than adding 'const'?

--
Ian Collins.
Ron Natalie
Guest
 
Posts: n/a
#8: Jan 25 '06

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


Stephan Brönnimann wrote:
[color=blue]
>
> 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.[/color]

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:
[color=blue]
> 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.[/color]
Stephan Brönnimann
Guest
 
Posts: n/a
#9: Jan 26 '06

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


Ron Natalie wrote:[color=blue]
> Stephan Brönnimann wrote:
>[color=green]
> >
> > 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.[/color]
>
> 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:
>[color=green]
> > An integral constant-expression can involve only literals (2.13), enumerators, const variables or static
> > data members of integral or enumeration types initialized with constantexpressions (8.5), non-type template
> > parameters of integral or enumeration types, and sizeof expressions.[/color][/color]

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

Closed Thread