1

I am currently in the process of learning C++ and have been reading C++ Primer (5th Edition).

In chapter 3.5 which talks about arrays and initializing them, it says arrays must be initialized using a constant expression.

Here is an example from the book

unsigned cnt = 42; // not a constant expression
constexpr unsigned sz = 42; // constant expression
int arr[10]; // array of ten ints
int *parr[sz]; // array of 42 pointers to int
string bad[cnt]; // error: cnt is not a constant expression
string strs[get_size()]; // ok if get_size is constexpr, error otherwise”

Excerpt From: Stanley B. Lippman. “C++ Primer, Fifth Edition.” 

However when I try this using g++ -std=c++11 everything compiles just fine. So I am kind of confused as whether this is just a mistake in the book or has the standard been modified since the writing of the book even though the book states it uses C++ 11.

Here is the actual code I am using which compiles and runs perfectly fine

unsigned int cnt = 42; // not constant expression
constexpr unsigned int sz = 42; // constant expression

int arr[10]; // array of 10 ints
int *parr[sz]; // array of 10 int pointers
string bad[cnt];

I even tried something like this

int var = 2;
int size = var;
int int_arr[size];

And this also works.

If anyone has an explanation or just why this works even though it states it shouldn't I would appreciate it.

Thanks!

Luis
  • 951
  • 2
  • 12
  • 27

1 Answers1

4

Variable length arrays are extensions that are built into the compiler for C++11 and are not standard, if you compile with the -Wvla flag you will see the compiler emit an error for your code.

You are better of using std::vector for dynamic arrays instead. If you are not familiar with how to use those I would suggest looking around online. There are plenty of good sources which go into detail about how to use std::vector and how it works.

Curious
  • 20,870
  • 8
  • 61
  • 146
  • Ahh, I do see a warning generated now when using this flag. I understand how to use vectors and the benifits of them, was just wondering why I wasn't getting any compiler errors for my code. So initializing an array with a variable is not a recommended thing to do? If so, what are the drawbacks in terms of performance, from what I understand normal arrays could be a bit faster than vectors since you know exactly how many elements you need and memory doesn't need to be reallocated when resizing, does using a variable for initialization slow this down? – Luis Jul 28 '17 at 01:57
  • I don't think it slows anything down but remember that with this method you are taking up memory on the stack. Which is constrained as compared to memory on the heap. Therefore you are more likely to stack overflow when using variable length arrays with big sizes. – Curious Jul 28 '17 at 02:02
  • Gotcha, also more reading has also stated that since this is a compiler extension (like you said above) this code may not work in other C++ implementations which is another severe draw back. Thanks again for the help! – Luis Jul 28 '17 at 02:04