开发者

Why are dynamic arrays needed in C++?

I don't under开发者_JS百科stand the need for dynamic arrays. From what I understand so far, dynamic arrays are needed because one cannot always tell what size of array will be needed at runtime.

But surely one can do this?:

cin >> SIZE;
int a[SIZE];

So what's the big deal with dynamic arrays and the new operator?


Firstly, that is a compiler extension and not Standard C++. Secondly, that array is allocated on the stack, whereas operator new allocates from the heap, which are two very different places that drastically affects the lifetime of the array. What use is that code if I want to return that array? Thirdly, what are you gonna do if you want to resize it?


SIZE is a variable which means it's value can be modified. Array by definition, can neither grow nor shrink in it's size. So, it's size needs to be a compile time constant.


cin >> SIZE; int a[SIZE];

Some of my users have a hard enough time using a mouse, and you want them to tell my application how much memory to allocate?


Dynamic arrays are very handy ... suppose you keep generating objects, and you have no idea how many objects might be generated (i.e., how much input might you get from someone typing an answer at a prompt, or from a network socket, etc.)? You'd either have to make predictions on what appropriate sizes are needed, or you're going to have to code hard-limits. Both are a pain and in the case of hard-limits on arrays, can lead to allocating way too much memory for the job at hand in order to try and cover every possible circumstance that could be encountered. Even then you could incur buffer-overruns which create security holes and/or crashes. Having the ability for an object to dynamically allocate new memory, yet keep the association between objects so that you have constant-time access (rather than linear-time access like you'd get with a linked-list), is a very, very handy tool.


In the early days of C, space was made on the stack when you first entered a function and the size of the stack was untouched until you called another function or returned. This is why all variables needed to be declared at the top of a function, and why array sizes needed to be known at compile-time, so the compiler could reserve a known amount of stack.

While C++ loosened the restrictions of variable declaration, it still kept the restriction of knowing the stack requirements at compile time. I don't know why.

As noted in the link in the comments, C finally allowed for dynamic size arrays. This came after C and C++ were split, so C++ didn't automatically gain that capability. It's not uncommon to find it supported as an extension in C++.


Personally I prefer:

std::cin >> size;
std::vector a(size);

Later, as others have mentioned, you could do something like ...

std::cin >> size;
a.resize(size);

...but, and this is probably the key point, you don't have to if you don't want to. If your requirements and constraints are such that they can be satisfied with static size arrays / vectors / other data structures then great. Please don't think you know enough about everybody else's requirements and constraints to remove from them a very useful tool.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜