Difference between a static array and a dynamic array

This topic is 1061 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hello,

I'm currently learning C++ and now I'm doing a chapter about dynamic memory.

My book says that there is one big difference between the use of a static array and a dynamic array:

You have to declare the "bigness" of a static array when you write the program, but with a dynamic array this can

be done in runtime. But I wondered, if you do this with a static array:

int amountOfValuesInArray;

cout << "How many values?" << endl;

cin >> amountOfValuesInArray;

int a[amountOfValuesInArray]

ETC.......

Then it is done in runtime too.

I think I do not understand something, could you explain me that (the difference between the arrays)

thanks.

Share on other sites
This is allowed in pure C but not currently in C++, although some compilers support it as an optional extension.

Share on other sites
Say you have a game, where level 1 needs 20 objects but level 2 needs 30.
How big would you make your array?

Option 1:
- a static array of 30 objects
- in level 1 keep a counter to prevent doing stuff with the non existing 5 objects in the array

Option 2:
- dynamic array

Option 2/ dynamic can be done by either manual allocation (using new/ delete) or using std library called vector. The latter has a lot of advantages depending of your goal, for example arrayname.size() returns it's size, easier sorting etc. Your example above can be done using std::vector, by addind objects/ values into the array without doing manual new allocation. Example:

Std::vector<int>myArray;

myArray.push_back(4);
myArray.push_back(7);

Int j = myArray.size(); // will return 2 here
Int i = myArray.back(); // will return 7
Int k = myArray[0]; // will return 4

You can even determine the sized based on external data, like level files. Giving you the possibility to create flexible data driven systems, using one piece of code.

Share on other sites

Hello,

I'm currently learning C++ and now I'm doing a chapter about dynamic memory.
My book says that there is one big difference between the use of a static array and a dynamic array:
You have to declare the "bigness" of a static array when you write the program, but with a dynamic array this can
be done in runtime. But I wondered, if you do this with a static array:

int amountOfValuesInArray;

cout << "How many values?" << endl;
cin >> amountOfValuesInArray;
int a[amountOfValuesInArray]
ETC.......

Then it is done in runtime too.
I think I do not understand something, could you explain me that (the difference between the arrays)
thanks.

That shouldn't actually compile with any sensible compiler.

With standard arrays the compiler needs to know the size at compile time, that means you have to provide a size which is a constant value(typing 7 for example would be a literal, and thus constant.) With your example you could make it compile only if amountOfValuesInArray was a const int and thus initialized with a value when the compiler needs to know the size.

Creating a dynamic array means having to use a pointer and then new to allocate memory when you need it of the appropriate size, this is what containers like std::vector do internally. They also free the memory when destroyed or when they need to resize, generally copying the elements to the new block of memory.

Keep in mind "static" in this context, refers to the size of the container not changing for the lifetime of the program, not necessarily the static keyword(which can create the same effect in C++ but also can be used for other purposes depending on context.) Edited by Satharis

Share on other sites

Hello,

I'm currently learning C++ and now I'm doing a chapter about dynamic memory.

My book says that there is one big difference between the use of a static array and a dynamic array:

You have to declare the "bigness" of a static array when you write the program, but with a dynamic array this can

be done in runtime. But I wondered, if you do this with a static array:

int amountOfValuesInArray;

cout << "How many values?" << endl;

cin >> amountOfValuesInArray;

int a[amountOfValuesInArray]

ETC.......

Then it is done in runtime too.

I think I do not understand something, could you explain me that (the difference between the arrays)

thanks.

In a word: resizablity.

Share on other sites

Okey thanks, I was confused, because it compiles without a problem in Code::Blocks, but now I understand that it isn't a "normal" feature of C++, but just something from Code::Blocks.

Share on other sites

ADD TO LAST REPLY: At least I think I understand it, if I understand it wrong, please correct me.

Share on other sites

Okey thanks, I was confused, because it compiles without a problem in Code::Blocks, but now I understand that it isn't a "normal" feature of C++, but just something from Code::Blocks.

Just to be clear, this isn't Code::Block's fault. Code::Blocks is the IDE you use, where you type code into (kinda like a Microsoft Word except for programming). It's the compiler's fault. The compiler is what changes the textual code into actual executables that runs on your machine.

Code::Blocks ships with very recent versions of MinGW compiler, and it's the MinGW compiler that is giving you that behavior. MinGW is an excellent compiler, so it's not like you need to change compilers or anything. I use MinGW also, and probably the exact same (or very close to) the version as you do - unless you installed Code::Blocks from a disc that came with your book, in which case it might be out-of-date. You can check what version of Code::Blocks you have installed, and if it's too out of date, you may want to install a more recent version (which will automatically install a more recent version of MinGW as well, if you download the right installer).

Most compilers have a few non-standard extensions added in, and this isn't a bad thing. Sometimes they add those non-standard extensions because they realize that C++ is going to add that feature soon anyway.

Other times, compilers add the non-standard features because many developers want them, and once several compilers implement it, it becomes "de-facto" standard (not actually standard but present everywhere anyway), and then the C++ committee may realize that it'd be a good idea to make it officially standard later.

Anyway, there's several differences between static arrays and dynamic arrays. Some static arrays can't be created at runtime. For example, std::array is always compile-time. std::array is another type of static arrays (or rather, a wrapper around static arrays).

But the more important difference is that, after being created (whether the creation size is chosen at runtime or compile), the same variable can change its size.

Example:

int Func(int size)
{
int myStaticArray[size]; //Always this size, can't change until the next time 'Func' is called.

std::vector<int> myDynamicArray(size);
myDynamicArray.resize(differentSize); //Dynamic arrays can be resized later.
}

There's other differences between these two arrays, which you'll learn as you continue programming.

The only difference for now that you need to realize between 'static' and 'dynamic' arrays, is that 'dynamic' can be resized later. Whether their sizes are set in stone at compile-time or run-time is really not too important.

In general, I just use std::vector (or std::array if I don't need it to resize) for almost every situation unless I have a specific reason why something else is better.

Edited by Servant of the Lord

Share on other sites
I'm not completely sure yet but I believe I was mistaken. It's quite possible that dynamic sized arrays with automatic storage are actually standard in C++14. Apparently there is at least this proposal which made it pretty far but it's getting a bit late to actually check what became of it for the final version. Anyone want to chime in?

I started getting suspicious after MinGW 4.9.1 even with als settings cranked up to 'as standard compliant as possible' accepted the code.

Share on other sites

What I understand now: My example isn't a 'normal' C++ thing, but just from MinGW (or maybe even C++14).

If this is right, then I understand my book again.

Share on other sites

While some implementations may have provided the mechanism, it is not part of the language standard.

Compilers are free to implement their own additional functionality beyond what the language standard provides. The differences are generally documented both so developers know they can use them and know how to avoid them if they choose.

is it possible that your compiler implements it as some sort of extension or bonus feature? Possibly. Even if it does, that does not make the feature part of the language.

Share on other sites

I'm not completely sure yet but I believe I was mistaken. It's quite possible that dynamic sized arrays with automatic storage are actually standard in C++14. Apparently there is at least this proposal which made it pretty far but it's getting a bit late to actually check what became of it for the final version. Anyone want to chime in?

IIRC, it was ultimately rejected from C++14 version of the standard (time constraints, more review needed), but still on track for the next version (C++17).

I started getting suspicious after MinGW 4.9.1 even with als settings cranked up to 'as standard compliant as possible' accepted the code.

Probably because it was expected to get in the standard, so (I'm guessing) they added it in. The compiler vendors of late have been racing each other to see who can implement the latest standards the soonest, which includes implementing features that aren't yet set in stone. This is good for us, though, because it means the compilers are making rapid progress in optimized output, standard-conformity, and feature additions.

[Edit:] Here's what GCC says:

GCC says: "This feature was briefly part of the C++14 working paper, but was not part of the published standard; as a result, it has been removed from the compiler."

Additionally, "G++ supports C++1y variable length arrays. G++ has supported GNU/C99-style VLAs for a long time, but now [GCC 4.9] additionally supports initializers and lambda capture by reference. In C++1y mode G++ will complain about VLA uses that are not permitted by the draft standard, such as forming a pointer to VLA type or applying sizeof to a VLA variable. Note that it now [GCC 5.0] appears that VLAs will not be part of C++14, but will be part of a separate document and then perhaps C++17."

The bold brackets with the GCC versions are my additions.

Edited by Servant of the Lord