views:

462

answers:

4

What is the difference between a static array and a dynamic array in C++?

I have to do an assignment for my class and it says not to use static arrays, only dynamic arrays. I've looked in the book and online, but I don't seem to understand.

I thought static was created at compile time and dynamic at runtime, but I might be mistaken this with memory allocation.

Can you explain to me the difference between static array and dynamic array in C++?

Thnaks.

+1  A: 

I think in this context it means it is static in the sense that the size is fixed. Use std::vector. It has a resize() function.

Eddy Pronk
+7  A: 

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

Michael Mrozek
This is correct, but only to illustrate how it works. Please don't do this in real code but use a std::vector instead.
Eddy Pronk
@Eddy: It depends on the situation as to whether a vector is necessary
Casebash
@Casebash: In which situation would you prefer an array? "You should always prefer to use vectors or deques instead of arrays." - Herb Sutter (More exceptional C++)
Eddy Pronk
If you are fine with setting the maximum size at creation time, then you don't need a vector. Does actually occur occasionally.
Casebash
@Casebash: In that case there is no extra cost in using a vector either.
Eddy Pronk
A: 

Static arrays are allocated memory at compile time and the memory is allocated on the stack. Whereas, the dynamic arrays are allocated memory at the runtime and the memory is allocated from heap.

int arr[] = { 1, 3, 4 }; // static integer array.   
int* arr = new int[3]; // dynamic integer array.
Jagannath
+1  A: 

I think the semantics being used in your class are confusing. What's probably meant by 'static' is simply "constant size", and what's probably meant by "dynamic" is "variable size". In that case then, a constant size array might look like this:

int x[10];

and a "dynamic" one would just be any kind of structure that allows for the underlying storage to be increased or decreased at runtime. Most of the time, the std::vector class from the C++ standard library will suffice. Use it like this:

std::vector<int> x(10); // this starts with 10 elements, but the vector can be resized.

std::vector has operator[] defined, so you can use it with the same semantics as an array.

Ben Collins
I think it's fairly clear that by "dynamic array" they simply mean a dynamically allocated array (that is, one in which the size can be specified dynamically, at runtime). Like `new int[10]`
jalf
@jalf: I was more concerned about the term 'static'. I prefer to call a "dynamic array" an allocated or variable size array for the sake of consistency.
Ben Collins