2

Why this type of declaration

int nArraySize = 7;
char szName[nArraySize] = "Mollie";

returns this error:

error: variable-sized object 'szName' may not be initialized

but when I declare the 'arraySize' variable as a 'const int' it works ?

const int nArraySize = 7;
char szName[nArraySize] = "Mollie";

5 Answers 5

3

It must be said first that in C++ language, the size part of array declaration is required to be an Integral Constant Expression (ICE). A const int object declared with an initializer can be used in an ICE. An int object cannot be used in an ICE. That's the formal part of it.

However, judging by the error message, your C++ compiler supports C99-style variable-length arrays (VLA) in C++, as a non-standard extension. That means that in your compiler you are allowed to use non-constant expressions to specify size in array declarations. Yet even if VLAs themselves are supported, such arrays still cannot be initialized. This is prohibited by the specification of VLAs in C99, and that is exactly how their specification is "inherited" by your C++ compiler.

In other words, contrary to what other answers stated, this code will probably be accepted by your C++ compiler

int nArraySize = 7;
char szName[nArraySize];

even though it is formally illegal C++. It is the = "Mollie" part that triggers the error.

Sign up to request clarification or add additional context in comments.

2 Comments

The relevant part of the C standard would be 6.7.9 (3): "The type of the entity to be initialized shall be an array of unknown size or a complete object type that is not a variable length array type."
Thank you for your answer; you were totally right about initialization
1

Because C++ does not support variable-length arrays (introduced in the C-99 standard, but not in any version of C++). When you declare nArraySize as a non const int, the compiler complains because nArraySize may change at runtime. If nArraySize is const, the compiler knows that it cannot change at runtime, and therefore the array size of szName cannot be variable (i.e. can be deduced at compile time). In C++ (and versions of C before C99), the size of an array must be a constant that can be deduced at compile-time.

1 Comment

This answer is perfectly accurate, but I believe the immediate cause for the actual error is different is OP's case. The error message indicates that the C++ compiler in question supports VLAs (as a non-standard extension). The error message simply states that VLAs cannot be intailized. This is true: even in C99 VLAs do not accept initailizers.
0

Because the program has to know at compile time how much memory to alocate for your variables. When you don't make your nArraySize constant it is assumed it may change during runtime. While making it constant asures the compiler this value will not be changed.

Comments

0

The first is a variable length array and it's standardized in C (since the C99 standard) but not in C++.

C++ needs all arrays to have their sizes available at compile time, not runtime. Declaring the size as a constant makes it a compile-time constant.

1 Comment

@Joachim Pileborg: Same problem. The error message actually indicates that this C++ compiler supports VLAs (as a non-standard extension). The error message simply states that VLAs cannot be intailized. This is true: even in C99 VLAs do not accept initailizers.
0

The Standard does not permit dynamically-sized, statically-allocated arrays. You may find that in GCC you will be able to do this, but that is because that is one of a number of extensions that allow non-conforming behavior.

An array is defined like this:

D1 [ constant-expressionopt] attribute-specifier-seqopt

Where the size is an integral constant expression. The Standard defines an integral constant expression like this:

An integral constant expression is an expression of integral or unscoped enumeration type, implicitly converted to a prvalue, where the converted expression is a core constant expression. [Note: Such expressions may be used as array bounds (8.3.4, 5.3.4), [...] — end note]

int n = 10;
int x[n]; // error!

The variable n is not a constant expression, so it will not work for this case.

By adding constexpr (C++11) to the type, it will be useable in a constant expression. But in this case const is enough to make it work:

int const n = 5;

int x[n];

On the other hand, dynamic arrays take a dynamic size specifier:

int n = 10;

int *x = new int[n];

But an option I would recommend using is std::vector which is a wrapper around a dynamically-sized buffer:

#include <vector>

int main()
{
    int n = 10;

    std::vector<int> x(n); // x.size() is 10
}

I hope this helped.

1 Comment

Same problem. The error message actually indicates that this C++ compiler supports VLAs (as a non-standard extension). The error message simply states that VLAs cannot be intailized. This is true: even in C99 VLAs do not accept initailizers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.