473,404 Members | 2,178 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,404 software developers and data experts.

size of array is not an integral constant-expression

#include <vector>
using namespace std;

template <typename Iter>
int
foo(Iter first, Iter last, int nn)
{
const size_t n = last - first;
double buf[n];
return 0;
}

int
main(int argc, char **argv)
{
vector<doublex;
foo(x.begin(), x.end(), argc);
return 0;
}

foo.cc:17: instantiated from here
foo.cc:9: error: size of array is not an integral constant-expression

g++ 4.2.1

Is this error specific to g++ 4.x? g++ 3.6.4 and g++ 2.9.5 have no
problems with it, but that doesn't mean they are right. Is there some
reason to expect this to fail.

There are a few interesting workarounds that point to this being
unexpected behavior... I'll post those next.
Mar 12 '08 #1
8 11842
jo*******@gmail.com wrote:
#include <vector>
using namespace std;

template <typename Iter>
int
foo(Iter first, Iter last, int nn)
{
const size_t n = last - first;
double buf[n];
return 0;
}

int
main(int argc, char **argv)
{
vector<doublex;
foo(x.begin(), x.end(), argc);
return 0;
}

foo.cc:17: instantiated from here
foo.cc:9: error: size of array is not an integral constant-expression

g++ 4.2.1

Is this error specific to g++ 4.x?
No, n is not a compile time constant, the error specific to any
conforming compiler!

The expression

const size_t n = last - first;

double buf[n];

declares (in C99) a variable length array, something gcc in default mode
has supported for years.

C++ does not have VLAs.

--
Ian Collins.
Mar 12 '08 #2
jo*******@gmail.com wrote:
#include <vector>
using namespace std;

template <typename Iter>
int
foo(Iter first, Iter last, int nn)
{
const size_t n = last - first;
double buf[n];
return 0;
}

int
main(int argc, char **argv)
{
vector<doublex;
foo(x.begin(), x.end(), argc);
return 0;
}

foo.cc:17: instantiated from here
foo.cc:9: error: size of array is not an integral constant-expression

g++ 4.2.1

Is this error specific to g++ 4.x?
Not that I can see. 'last - first' is a run-time expression. And when
you use it to initialise a 'const size_t', the variable ('n') also
becomes a run-time expression. It cannot be used to declare an array.
g++ 3.6.4 and g++ 2.9.5 have no
problems with it, but that doesn't mean they are right. Is there some
reason to expect this to fail.
Yes, there is. It goes against the rules of the language. The older
versions of G++ may have had it as an extension. Hell, the new versions
may still have it as an extension, and you're welcome to use it, just
don't claim your program to be C++.
There are a few interesting workarounds that point to this being
unexpected behavior... I'll post those next.
What's so unexpected in actually implementing the rules of the language?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 12 '08 #3
So, it seems that adding -pedantic gives:
foo.cc:10: error: ISO C++ forbids variable-size array 'buf'
Sorry for the noise. Somewhere along the way I had gotten the
impression that the c++ standard included support for variable length
arrays. Apparently not - just a gcc-ism.
Mar 13 '08 #4
jo*******@gmail.com wrote:
I'm not sure why everyone is giving the big thumbs down to run time
array sizing. C++ doesn't have a problem with the concept in
general. As mentioned by someone, C99 has some support for it, too,
but that's not directly relevant to the issue I've just noticed.
It isn't part of the language.

All the things you have noted are specific gcc behaviour when that
compiler isn't used in compliant mode.

If you use gcc, use

g++ -ansi -pedantic -Wall

--
Ian Collins.
Mar 13 '08 #5
On Mar 12, 9:22 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
What's so unexpected in actually implementing the rules of the
language?
The fact that almost no compiler does? How many support export,
for example? If I base my expectations on practical experience,
I expect that any given compiler is likely to deviate from the
rules in various places.

It makes writing truely portable code very hard.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Mar 13 '08 #6
James Kanze wrote:
On Mar 13, 12:45 am, johneh...@gmail.com wrote:
As mentioned by someone, C99 has some support for it, too, but
that's not directly relevant to the issue I've just noticed.

For whatever reasons, the C++ standards committee decided not to
adapt this feature of C99.
They probably felt std::vector was a better solution.

Brian
Mar 13 '08 #7
Vidar Hasfjord wrote:
Aside: Many C++ implementations provide a stack allocation feature as
an extension (alloca).
What is the purpose?
Mar 18 '08 #8
Jeff Schwab wrote:
Vidar Hasfjord wrote:
>Aside: Many C++ implementations provide a stack allocation feature as
an extension (alloca).

What is the purpose?
Much the same as VLAs, to save the cost of a dynamic allocation.
alloca shares the biggest drawback of VLAs - lack of failure notification.

--
Ian Collins.
Mar 18 '08 #9

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

Similar topics

14
by: Gianni Mariani | last post by:
Does anyone know if this is supposed to work ? template <unsigned N> int strn( const char str ) { return N; } #include <iostream>
7
by: news.hku.hk | last post by:
suppose i have a class abc, and i have an important value stored as int integer = 888; when i want to declare an array of objects: abc obj; // error said non-constant....expect constant...
22
by: Wynand Winterbach | last post by:
I think every C programmer can relate to the frustrations that malloc allocated arrays bring. In particular, I've always found the fact that the size of an array must be stored separately to be a...
4
by: Bilgehan.Balban | last post by:
Hi, The following code: #include <stdio.h> // const int const_asize = 10; #define define_asize = 10; int array = {1,2,3,4,5,6,7,8,9,0};
9
by: joshc | last post by:
Hi, I have an array defined in one file with an intializer as follows: int arr = {0, 1, 2, 3}; I have a declaration of the array in another file as follows: extern int arr;
78
by: Frederick Gotham | last post by:
On modern 32-Bit PC's, the following setup is common: char: 8-Bit short: 16-Bit int: 32-Bit long: 32-Bit "char" is commonly used to store text characters. "short" is commonly used to store...
3
by: johnmmcparland | last post by:
Hi all, I would like to have a static constant array inside a class definition which would contain the number of days in each month (I am writing a Date class as an exercise). However my...
6
by: lazy | last post by:
Hi, Im trying to define a hashtable thats static(meaning the table is initialised at compile time itself) It takes a char and has 2 other ints as values For eg: arr={1,2}...
36
by: James Harris | last post by:
Initial issue: read in an arbitrary-length piece of text. Perceived issue: handle variable-length data The code below is a suggestion for implementing a variable length buffer that could be used...
33
by: Adam Chapman | last post by:
Hi, Im trying to migrate from programming in Matlab over to C. Im trying to make a simple function to multiply one matrix by the other. I've realised that C can't determine the size of a 2d...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.