Jump to content
  • Advertisement
Sign in to follow this  
Dalphin

Difference between a static array and a dynamic array

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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
- allocate memory for 25 objects when loading level 1
- delete/ reallocate memory for 30 objects when loading level 2

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!