开发者

How is a dynamic array different from statically bound array in C++?

I am learning C++ and I just read about dynamic arrays and how it lets you set the length of an array during runtime rather than during compile time. However, you don't need a dynamic array to do this. So what is the point of a dynamic array; when would you use it? I feel like I am missing somethi开发者_JS百科ng obvious so any insight is much appreciated. Thanks!

// Static binding.
int size = 0;

cout << "Enter size of array:" << endl;
cin >> size;

int array[size];

int array_length = sizeof(array) / sizeof(int);
cout << "Number of elements in array: " << array_length << endl;

// I just set the length of an array dynamically without using a dynamic array.
// So whats the point of a dynamic array then?


I don't think you can do that in C++. Only C99 allows variable-length arrays.

Does this even compile? Were you talking about the vector class?

EDIT:

It does not compile in Visual Studio 2010:

1>..\main.c(207): error C2057: expected constant expression
1>..\main.c(207): error C2466: cannot allocate an array of constant size 0
1>..\main.c(207): error C2133: 'array' : unknown size
1>..\main.c(209): error C2070: 'int []': illegal sizeof operand


You would need a dynamically allocated array for cases where you don't know ahead of time how many items you will have.

Another (and better) option is to use std::vector.


As per the standard, an array defined "statically" would:

  1. have a constant size, defined at compile time
  2. Be allocated on the stack rather than the heap.

So the reasons you'd opt for a dynamically allocated array (using new type[]) are because you don't always know the size you need for an array at compile time, and the stack is a limited resource that you often need to be careful not to exhaust.

But in practical terms, you'd often be best served by using std::vector or other STL container instead of a builtin array of any sort.


Your example would not compile under a standards-compliant compiler. For instance, GCC says this:

http://codepad.org/Kvq2PfNx

In function 'int main()':
Line 13: error: ISO C++ forbids variable-size array 'array'
compilation terminated due to -Wfatal-errors.

There is a way of dynamically allocating stack memory using the alloca function. However, this memory is invalidated when the function terminates. You should generally prefer new[]/delete[] or std::vector for dynamic memory allocation.


You can dynamically create an array using the keyword new.. Lets say you don't know ahead of time how many values you need. It's a waste of space to declare an array a[100] when the user might just enter a few values. Also the user might enter more values, and then you would have a array overflow error. You can create an array dynamically like - int a = new int[]
Also, statically created arrays are created on the stack where as dynamically created arrays are created on the heap-which means the memory is globally available even after the function goes out of scope.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜