Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 mistaking this with memory allocation.

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

Thanks.

share|improve this question
    
Static is not the opposite of dynamic. Either the book you are using is terrible, or you are taking it out of context. I'm going to add a new answer below to hopefully clear this up. –  Joshua Clayton Jul 23 '13 at 15:12
    
See the diagram in this question: stackoverflow.com/a/11698458/1143274 Static arrays are not allocated on the stack or the heap. –  Evgeni Sergeev Dec 26 '13 at 1:24
add comment

8 Answers

up vote 31 down vote accepted

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

share|improve this answer
9  
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 Apr 20 '10 at 2:22
10  
@Eddy: It depends on the situation as to whether a vector is necessary –  Casebash Oct 14 '10 at 2:07
1  
@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 Oct 15 '10 at 0:27
9  
@EddyPronk For memory fragmentation reasons one may use a fixed array as a sort of pool. Not every case demands the heap, there are special benefits to using stack based arrays. You're treating the std::vector as a golden hammer, a common anti-pattern. –  Robert Dailey Oct 10 '11 at 19:16
2  
@EddyPronk: I'm pretty sure Herb Sutter meant dynamic arrays, like int* foo = new int[N] which you have to delete yourself and hence be careful in presence of exception. Static arrays don't have these problems. –  Alexander Malakhov Feb 11 '13 at 9:23
show 4 more comments

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.

share|improve this answer
1  
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 Apr 20 '10 at 2:59
    
@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 Apr 23 '10 at 14:58
add comment

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.

share|improve this answer
add comment

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.
share|improve this answer
add comment

Yes right the static array is created at the compile time where as the dynamic array is created on the run time. Where as the difference as far is concerned with their memory locations the static are located on the stack and the dynamic are created on the heap. Everything which gets located on heap needs the memory management until and unless garbage collector as in the case of .net framework is present otherwise there is a risk of memory leak.

share|improve this answer
add comment

static is a keyword in C and C++, so rather than a general descriptive term, static has very specific meaning when applied to a variable or array. To compound the confusion, it has three distinct meanings within separate contexts. Because of this, a static array may be either fixed or dynamic.

Let me explain:

The first is C++ specific:

  • A static class member is a value that is not instantiated with the constructor or deleted with the destructor. This means the member has to be initialized and maintained some other way. static member may be pointers initialized to null and then allocated the first time a constructor is called. (Yes, that would be static and dynamic)

Two are inherited from C:

  • within a function, a static variable is one whose memory location is preserved between function calls. It is static in that it is initialized only once and retains its value between function calls (use of statics makes a function non-reentrant, i.e. not threadsafe)

  • static variables declared outside of functions are global variables that can only be accessed from within the same module (source code file with any other #include's)

The question (I think) you meant to ask is what the difference between dynamic arrays and fixed or compile-time arrays. That is an easier question, compile-time arrays are determined in advance (when the program is compiled) and are part of a functions stack frame. They are allocated before the main function runs. dynamic arrays are allocated at runtime with the "new" keyword (or the malloc family from C) and their size is not known in advance. dynamic allocations are not automatically cleaned up until the program stops running.

share|improve this answer
add comment

Static array :Efficiency. No dynamic allocation or deallocation is required.

Arrays declared in C, C++ in function including static modifier are static. Example: static int foo[5];

share|improve this answer
    
This question was answered and accepted years ago. –  admdrew Dec 18 '13 at 18:20
add comment

static arrary meens with giving on elements in side the array

dynamic arrary meens without giving on elements in side the array

example:

     char a[10]; //static array
       char a[];  //dynamic array
share|improve this answer
    
This is absolutely incorrect... –  Matthew Jan 23 at 18:50
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.