Take the 2-minute tour ×

How is using 'new' considered dynamic? I understand that it gets put on the heap, returns a pointer and sticks around, but I don't yet recognize it's "dynamic" nature in terms of allocation.

In the situations below, both arrays are allocated at runtime even the static method. So I don't see a difference. And with both I don't believe its possible to grow/shrink the allocation dynamically, correct?

int size = 0;
std::cout << "enter size: ";
std::cin >> size;

int* array_d = new int[size];   // "dynamic" allocation
int array_s[size];              // static allocation correct?
share|improve this question
dynamic and static are used trivially in this example. This "static" allocation has noting to do with actual static allocation - it just implies the number is a constant. (btw, the code is invalid because size isn't constant, so the arrray_s is wrong) –  Luchian Grigore Jul 9 '13 at 8:32
Neither of these is code you should ever use in C++ – but for very different reasons (one is illegal, the other just bad). –  Konrad Rudolph Jul 9 '13 at 8:34
the code compiles and runs for me (gcc version 4.2.1). why are both bad code? also I'm still not sure how 'new' is dynamic as both are set at runtime and neither can grow/shrink at runtime. –  user1229895 Jul 9 '13 at 9:39
@user1229895 It doesn’t compile when you pass the option -pedantic. The code is illegal C++, GCC just happens to allow it anyway. new, being manual memory management, is fragile and error-prone and has no part in modern C++. –  Konrad Rudolph Jul 9 '13 at 12:39
@KonradRudolph thanks for pointing that out, what should be used instead of <code>new<code> then? –  user1229895 Jul 9 '13 at 23:22

4 Answers 4

up vote 1 down vote accepted

Memory allocated with new is dynamic in the sense that the amount of memory to allocate is decided at run-time. As a result there is no guarantee that it will succeed.

A static array declaration has a constant size that is determined at compile time, and you typically wouldn't worry about that allocation failing in any way.

That said, a local array that is allocated on the stack could potentially fail if there is a stack overflow, but that is usually less of a concern.

share|improve this answer
Thanks for pointing out the potential pitfalls. I see that its dynamic because its set at runtime, but technically isn't that the case with both examples? –  user1229895 Jul 9 '13 at 9:51
Using a non-const size in an array declaration is a feature of C99 (variable length arrays). I don't believe it is valid in C++ (if you need that kind of functionality you should be using a vector) but GCC supports it as an extension. So, if you're declaring arrays like that then technically they are both dynamic, but in standard C++ and earlier versions of C that would not have been possible. –  James Holderness Jul 9 '13 at 10:15
thank you, your comment is the answer...it clears things up and I appreciate the vector suggestion. –  user1229895 Jul 9 '13 at 23:26

There are three types of memory allocation in C++. There's static allocation:

int x[100];

void foo() {
    static int y[100];

Here, the sizes of the arrays are known at compile time and there is one instance of each array per process, so the arrays can literally be statically allocated by the compiler: they can get a fixed position in the binary and the address space of the resulting processes.

Then there's automatic allocation:

void foo() {
    int z[100];
    // or even:
    //auto int z[100];
    // though nobody writes that

This is roughly in between static and dynamic. The size must be known at compile time, but there's one instance per function call. Typically, the instances are allocated near the current top of the stack.

Finally, there's dynamic allocation:

void foo(size_t n) {
    int *p = new int[n];
    delete[] p;

Here, the size and position in memory of the array are both determined dynamically, i.e. at runtime.

share|improve this answer
Thanks for addressing the different allocation. So its only dynamic in the sense that its set at runtime, not that it can grow dynamically? and aren't both examples in effect set at runtime? –  user1229895 Jul 9 '13 at 9:49
@user1229895: which examples? There are three of them. And no, allocations cannot grow with new. –  larsmans Jul 9 '13 at 10:39
sorry I meant of the two examples I gave. Though, I'm told one of them is technically invalid under normal operations. –  user1229895 Jul 9 '13 at 23:25
int array_s[size]; 

This is not valid c++, at least not in c++03/11, Arrays must have a fixed size that are allocated by reserving space on the stack which is usually allocated at compile time.

The first one is dynamic because you can allocate varying amounts of memory at run time, the second isn't valid c++ at all, only fixed amounts are legal, which is why it's not considered dynamic

share|improve this answer
received no warnings with gcc version 4.2.1. so declaring 'const int size;' would make the second example 'int array_s[size];' valid? –  user1229895 Jul 9 '13 at 9:43
That would make it valid. gcc has a non standard extension that lets you use non-const values for the size, but that's not standard c++ –  jcoder Jul 9 '13 at 9:52
thanks for confirming. –  user1229895 Jul 9 '13 at 23:28

The size of the first array may be changed at runtime, so it is "dynamic". The size of the second array must be known at compile time, thus it is "static".

share|improve this answer
Uh, no, the array may be allocated again, with a different size, but once an allocation is made, it's size is fixed. It's dynamic in the sense that the memory was not allocated at "build-time" (of course, that also applies to the second case, but that's not valid standard C++). –  Mats Petersson Jul 9 '13 at 8:38
You're right, what I wanted to say is that the size argument may change before allocation time, and does not need to be known at compile time. –  Steffen Jul 9 '13 at 8:58
so an allocation was made initially to 'int array_s[size];' with a default value for size during compile? and then at runtime cin overwrote it dynamically? doesn't that achieve what new is suppose to do (without the extras of it returning a pointer and being on the heap)? –  user1229895 Jul 9 '13 at 9:46

Your Answer


