Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old January 25th, 2006, 08:35 AM
amjain.gzb@gmail.com
Guest
 
Posts: n/a
Default Solaris : Difference in g++ and CC compiler for variable length array declaration

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

  #2  
Old January 25th, 2006, 09:15 AM
Ian Collins
Guest
 
Posts: n/a
Default Re: Solaris : Difference in g++ and CC compiler for variable lengtharray 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.
  #3  
Old January 25th, 2006, 09:35 AM
Ian Collins
Guest
 
Posts: n/a
Default Re: Solaris : Difference in g++ and CC compiler for variable lengtharraydeclaration

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.
  #4  
Old January 25th, 2006, 10:45 AM
John Carson
Guest
 
Posts: n/a
Default Re: Solaris : Difference in g++ and CC compiler for variable lengtharray 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


  #5  
Old January 25th, 2006, 07:15 PM
amjain.gzb@gmail.com
Guest
 
Posts: n/a
Default Re: Solaris : Difference in g++ and CC compiler for variable lengtharray 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]);

}

  #6  
Old January 25th, 2006, 08:05 PM
Stephan Brönnimann
Guest
 
Posts: n/a
Default Re: Solaris : Difference in g++ and CC compiler for variable lengtharray 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

  #7  
Old January 25th, 2006, 08:45 PM
Ian Collins
Guest
 
Posts: n/a
Default Re: Solaris : Difference in g++ and CC compiler for variable lengtharraydeclaration

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.
  #8  
Old January 25th, 2006, 11:05 PM
Ron Natalie
Guest
 
Posts: n/a
Default Re: Solaris : Difference in g++ and CC compiler for variable lengtharraydeclaration

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]
  #9  
Old January 26th, 2006, 08:45 AM
Stephan Brönnimann
Guest
 
Posts: n/a
Default Re: Solaris : Difference in g++ and CC compiler for variable lengtharray 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

 

Bookmarks

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles