We can use the inverse of the dynamic array to determine what features the dynamic array should have. We all know that the following way is to define a static array.
int iCount[10];
int iCount[10][10];
As you can see from the above, after you define a static array, the position is fixed regardless of whether the program makes the array the size of the space it occupies in memory.
We can conclude that for the compiler, the size and space of the static array are known, so the compiler can automatically allocate space for the array. The situation is: if you define a global array, the compiler assigns a space to your array in the data area, and if it is a local array (for example, in a certain number of innings), the compiler assigns a stack space to your array.
From the discussion of the static array, we derive the characteristic that the dynamic array should have: in the running of the program, the dynamic array should be of variable size. The implementation of some dynamic group numbers should be based on the dynamic allocation of memory base. Here's a look at this example:
Suppose we set up a database of factory workers, and there are multiple tables in the database representing different workshops. Each table holds the worker's information for the workshop, and for the sake of simplicity, the database can be saved only by keeping the employee's name.
The following is a inputworkers function that stores employee names in a shop-floor unit and then deposits the data into the database at once.
void InputWorkers()
{
int iCountOfWorkers, int iNo;
……
用户输入获得车间的人数和车间号
……
string* iArray = new string[iCountOfWorkers];
……
用户输入车间所有职工的信息,并存在iArray数组中
……
StoreInDatabase(iArray, iNo ); //存入数据库
delete [] iArray;
}
IArray is a string pointer in the program, not an array. But the principle of the array is the same as the pointer, for example, P[1] is the second element in the exponential group P, but in the actual addressing it is done in p+1. So we can use iarray[1].
The IArray in Inputworkers allocates space of different sizes according to the total number of workshops. In this sense, it can be thought that IArray realizes the function of dynamic array.
If the iarray is defined as a static array, then the size of the iarray is fixed, so we have to estimate an upper limit for the number of workshops.
string iArray[100];
The speed of a static array is faster than a dynamic array. Because theoretically, stacks are faster than heaps in speed. But if we decide to use a dynamic array because of space saving considerations. Also pay attention to the cost of the static array cap changes. We have to reset the ceiling to fix this bug and then recompile the program. If you can compile the program, that's fine. But you have to do it for every user to update the program. Users who do not have updates can encounter this bug. When you think about it, you can't be happy.
You might say, I set a larger limit, the likelihood of exceeding it is very small, and the waste of memory will not be much. For example, at most one workshop 200 people, at least one workshop 100 people, that also only wasted 100 space. Now the machine's memory does not care about such a space waste. Yes, you can do this, but please continue to discuss it down.
Now we want to store the names of all the employees in a two-dimensional array, each row of the array represents a workshop, and the element in each row is the employee's name. Think about it, if you use a static array, you'll waste more space. And you have to add a cap to the number of workshops. This example is not good, because the number of workshops in the factory should be OK. But I can put it another way, I only need a few workshops, but also may be all workshops, then do you still insist?
Said the above, just a little less discussed the dynamic array may be used. In reality, especially in large software systems, the use of dynamic arrays is actually very common. and in C + + libraries also have the implementation of the array of classes, by calling the corresponding class functions can be added/deleted elements in the array. And can also be nested to achieve two-dimensional dynamic. These classes or class templates are easy to use. Like what:
CAtlArray<int> iArray;
iArray[0] = 1; // 出错,iArray中并没有元素
iArray.Add(1); // element 2
iArray[0] = 1; // 可以,iArray中并有1个元素
iArray[0] = 1; // 出错,iArray中并只有1个元素