Small issues with the C Array
The dynamic array mentioned here is an array that can dynamically increase memory usage as needed. For example, the program initially allocates 100 elements, but after a period of operation, the 100 space in the zone cannot meet the requirements, now we need 400. What should we do? We need to allocate another 300.
The C language has the realloc () function to solve the space expansion problem, but do not forget that realloc may migrate the memory. In many cases, the elements in the array will be referenced by other functions/modules, if the address changes, the result will be disastrous.
What about STL vector? It also has the same problem.
It is wise to allocate enough space at a time to solve this problem. It is obvious that this will cause a waste of memory.
Do not use arrays? Using list can solve some of the problems, but list does not support random access. In view of the efficiency, it is clear that the array cannot be replaced by list.
How can this problem be solved? Dynamic Array! The dynamic array is used in the Demutex Table of HPServer. It turns out that the effect is good.
Dynamic Array features
Dynamic Array is a simple and easy-to-use data structure, but its simplicity does not represent a small advantage. Its features are as follows:
1. dynamically increase memory size in batches as needed;
2 once allocated, the element address will not change again;
3. Simple implementation and high efficiency. In fact, it has no efficiency loss compared with ordinary arrays;
4. the maximum number is fixed;
In fact, the most important thing is Feature 2. Otherwise, it is more convenient to directly use realloc. Of course, the implementation of dynamic arrays is also very convenient. We will discuss it in detail below.
Feature 4 is actually a limitation, but believe me, your program cannot reach this maximum.
Dynamic Array Implementation
As mentioned above, the implementation of dynamic arrays is very simple. The following assumes that the array element type is T. A secondary data structure is required first.
[Cpp]
Struct ARRAY_ELE_S
{
T item_array [1024];
};
ARRAY_ELE_S * parray[ 2000];
Int iSize;
The variable pArray is a pointer array of the ARRAY_ELE_S type, which is your dynamic array. iSize records the size of the current array.
The code above indicates:
1. the array dynamically increases by 1024 elements each time;
2 The maximum number of elements in the array can be: 2000*1024. If this is not enough, you can change the value to a greater value.
First, let's take a look at the memory usage. pArray itself occupies 2000*4, which is about 8 KB of memory and can be ignored.
If you allocate an array of 2000*1024 [2000*1024] at a time, the memory to be allocated at a time is 2 * sizeof (T) M, causing serious waste of space.
Let's take a look at the efficiency. Next we will take a look at the efficiency of the array's most important operation-random access. Next we will look at how it grows dynamically as needed.
Random Access
It takes two steps to access the array elements at the specified index position. First, locate the child array on which the element is located, and then locate the child array elements. In fact, it is very simple.
[Cpp]
T * get (int idx)
{
// Make sure the index is valid
If (idx> = 0) & (idx
{
Int idx_maj = idx/1024; // Primary Index
Int idx_mor = idx-(idx_maj); // secondary index
Return (& (pArray [idx_maj]-> item_array [idx_mor]);
}
Return NULL;
}
Item_array [idx_mor]);} return NULL ;}
Dynamic Growth
If the current space is not enough, the array needs to be dynamically increased. Otherwise, what is dynamic? The basic idea is that if the size required exceeds the current array size, the array needs to be increased, the Code is as follows.
[Cpp]
Int reclac (int size)
{
If (size> = 1024*2048) return-1; // too large
While (iSize <= size)
{
// Allocate memory and initialize it to 0
Int idx = iSize/1024;
PArray [idx] = (ARRAY_ELE_S *) calloc (1, sizeof (ARRAY_ELE_S ));
If (pArray [idx] = NULL)
{
Return (-1 );
}
ISize ++ = 1024;
}
Return 0;
}