473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
am********@gmail.com

Jan 25 '06 #1
8 7208
am********@gmail.com wrote:
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.

--
Ian Collins.
Jan 25 '06 #2
Ian Collins wrote:
am********@gmail.com wrote:
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.

--
Ian Collins.
Jan 25 '06 #3
"Ian Collins" <ia******@hotmail.com> wrote in message
news:11***************@drone2-svc-skyt.qsi.net.nz
Ian Collins wrote:
am********@gmail.com wrote:
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."

--
John Carson
Jan 25 '06 #4
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]);

}

Jan 25 '06 #5
John Carson wrote:
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

Jan 25 '06 #6
am********@gmail.com wrote:
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'?

--
Ian Collins.
Jan 25 '06 #7
Stephan Brönnimann wrote:

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

Jan 25 '06 #8
Ron Natalie wrote:
Stephan Brönnimann wrote:

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


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

Jan 26 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Dave Harrison | last post by:
Ok before I start, please dont mail me telling me to use a more recent version of Python, I _would_ use 2.2.x but due to an existing framework all based on using 2.1.1 I have been specifically told...
0
by: Jim Phelps | last post by:
After having memory leak issues with Xerces-c 2.3.0 for Solaris 2.7 for CC 6.2 I have decided to update to at least 2.4. I have downloaded the binary tarball and have installed it on my...
7
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b;...
0
by: Kirt Loki Dankmyer | last post by:
So, I download the latest "stable" tar for perl (5.8.7) and try to compile it on the Solaris 8 (SPARC) box that I administrate. I try all sorts of different switches, but I can't get it to compile....
38
by: Zytan | last post by:
What is the difference between these two lines? Dim args As Object() = New Object() {strText} Dim args As Object() = {strText} args seems usuable from either, say, like so: ...
1
by: dileepd | last post by:
Hi Every one, Could you please let me know if you have any clue about following things. 1. Whether type bool acts like a kind of signed int type in g++ on solaris 9(a/c to my invetsigation it...
45
by: anto frank | last post by:
hi friends, is ther any difference in array in c and array in c++?
7
by: Cromulent | last post by:
In section 6.7.5.2 it states the following: If the size is not present, the array type is an incomplete type. If the size is*instead of being an expression, the array type is a variable length...
11
by: sunnyalways4u2000 | last post by:
hello sir, Sir will please tell me the exact difference between C and advanced C...what are the extra features or funcions...etc added in this advanced one.
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.