Hi,

This is a very simple question, but I just couldn't figure out any set of keywords that would point to an obvious answer. So here we go.

Code example:

int array ;

Size is, for example, given as an argument.

Is this array initialization static or dynamic? It does not use 'new' statement but neither is the size a predefined value, which it is in every example I've found. Of course during runtime the size is known at the point of declaration. Thus, is that the only difference between the usage of static and dynamic, that at the point of declaration you don't need to give size for dynamic arrays?

I hope I managed to spit out my thoughts in an understandable manner.

Recommended Answers

All 3 Replies

Is this array initialization static or dynamic?

C++ does not allow an array with a size that is not constant. If your compiler compiles that declaration and it runs, then the documentation will tell you how it works. I assume it has the same behavior as an array created with alloca(). alloca() is not a standard C++ function, by the way. ;)

The new c++ standards, which will not be released for some time yet, is supposed to allow arrays to be allocated like that: int ay[size] where size is not a const integer. A few compilers may already support it, but if they do it is compiler-specifc.

>Is this array initialization static or dynamic?
It's called a variable-length array, and as far as you're concerned, it's static. However, as has already been noted, the current C++ standard doesn't support variable-length arrays (C99 does though).

>Thus, is that the only difference between the usage of static and dynamic,
>that at the point of declaration you don't need to give size for dynamic arrays?
No. Static memory is usually defined as stack-based memory, whereas dynamic memory is heap-based.

Where this gets tricky is variable-length arrays. Normally variables allocated with new and delete are considered dynamic, and anything else is static. But since variable-length array sizes are not known at runtime, a compiler may choose to allocate the memory in the stack or the heap. This is implementation-specific, for example gcc (under C99 mode) allocates variable-length arrays statically.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.