6

When I was reading about the array initialization in this tutorial. I found out this note.

type name [elements];

NOTE: The elements field within square brackets [], representing the number of elements in the array, must be a constant expression, since arrays are blocks of static memory whose size must be determined at compile time, before the program runs.*

As I know array allocate the memory in the run time. This should be a false note? or what does it means?

5
  • 1
    No, the note is correct and clear. It doesn't say that anything gets allocated at compile time. It says that the size is defined at compile time. The actual array will be allocated at runtime, using the size defined at compile time Commented Oct 3, 2014 at 8:47
  • 1
    If you tried it with a run-time variable providing the value for elements and it "worked" you're using a compiler that supports VLAs in C++ (such a gcc). And though it may "work", it isn't standard. The note is accurate. If you want runtime-sizing, use a std::vector<> and heap-managed data. Commented Oct 3, 2014 at 8:52
  • 2
    You seem to be confusing statically allocated arrays (int foo[sz];) with dynamically allocated arrays (int *foo = new foo[sz]). The former must have its size known at compile time; the latter can have its size determined at runtime. Commented Oct 3, 2014 at 9:09
  • Ok, Got what does it means. but what is the reason that stack array allocation strictly to static allocation in C++ standards? Commented Oct 3, 2014 at 9:16
  • @NayanaAdassuriya: Becuase there's std::vector<> in C++, which is often a better alternative. (There's also std::array<Type, Count> which is stored like Type[Count] but has .size(), .begin(), .end() etcetera) Commented Oct 3, 2014 at 11:00

3 Answers 3

6

Please check if the following answers help in giving you clarity about this.

Static array vs. dynamic array in C++

Static arrays are created on the stack, and necessarily have a fixed size (the size of the stack needs to be known going into a function): int foo[10];

Dynamic arrays are created on the heap. They can have any size, but you need to allocate and free them yourself since they're not part of the stack frame: int* foo = new int[10]; delete[] foo;

You don't need to deal with the memory management of a static array, but they get destroyed when the function they're in ends

Array size at run time without dynamic allocation is allowed?

C99 standard (http://en.wikipedia.org/wiki/C99) supports variable sized arrays on the stack. Some of the compilers might implement these standards and support variable sized arrays.

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

Comments

2

The declaration T a[N] requires that N be a converted constant expression.

Converted constant expression is an expression implicitly converted to prvalue of type T, where the converted expression is a core constant expression. If the literal constant expression has class type, it is contextually implicitly converted to the expected integral or unscoped enumeration type with a constexpr user-defined conversion function.

An int literal such as 5 is a prvalue, so can be used in the declaration T a[5], but an lvalue, for example int n = 5 cannot be used in the declaration T a[n], unless the lvalue under-goes an implicit lvalue-to-rvalue conversion where the lvalue:

a) has integral or enumeration type, is non-volatile const, and is initialized with a constant expression, or an array of such (including a string literal)

b) has literal type and refers to a non-volatile object defined with constexpr or to its non-mutable subobject

c) has literal type and refers to a non-volatile temporary, initialized with a constant expression

Therefore the following are valid:

const int n = 5; 
int a[n]; 
constexpr int n = 5; 
int a[n];

Comments

1

You may use :

int array[42];

but not

int n;
std::cin >> n;
int array[n]; // Not standard C++

the later is supported as extension by some compiler as VLA (Variable length array)

Comments

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.