The role of a dynamic array of C ++

If you want to create an array, the array length by the user input
it can be written:
int bufferSize;
cin >> bufferSize;
int * = the p-new new int [bufferSize];

however, if you use static arrays to achieve this function to ask how to achieve?
bufferSize int;
cin >> bufferSize;
int arr [bufferSize]; it? The system not compile a

static array is a stack allocated on, at compile time already determined the size (VC default stack size case is 1M)
dynamic arrays of storage space in the heap allocation on
only when the code runs before the operation The system application memory
machine how much available memory, you can apply much memory

Static is to say when the program has not yet decided to run, the program is dynamically determined during operation.

Here is needed to specify how much the length of the program is running, on demand, while static arrays are good you will determine the length of the program can not be changed during operation.

For example, you have several sets of data processing, uncertain data size when writing a program, there may be 10, it could be 1 million, then if you have a static array according to the maximum allocation, assignment 1,000,000, but this procedure most of the time only processing data size 10, wasting a lot of space.
With dynamic arrays (dynamic memory allocation), we can be allocated as needed, run out after the release, saving memory resources.

发布了359 篇原创文章 · 获赞 186 · 访问量 89万+

Guess you like

Origin blog.csdn.net/txwtech/article/details/104094336