[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]
We know that the space in the memory is continuous. That is to say, the address below 0x00000001 must be 0x00000002. Therefore, there will be no sudden changes in the address in the space. So what data structure type is continuous internal space? It is actually an array, and it can also be a heap. Arrays have many advantages. They can store the same type of data in a continuous space and manage the data. In this sense, you can get started with the data structure only after you have mastered the array.
So what should we pay attention to in linear structures during actual development? My personal point of view:
(1) The array resources are limited and the resource range must be determined.
(2) The application and release of resources in the array must correspond one to one. Otherwise, resource leakage may occur easily.
(3) Considerations in the array are also applied to the heap-allocated continuous memory resource space.
The following is a self-designed small int allocation program. You can try it together:
A) design the data format of Memory nodes
Typedef struct _ DATA_NODE
{
Int * pData;
Char * pFlag;
Int num;
} DATA_NODE;
# Define STATUS int
# Define TRUE 1
# Define FALSE 0
Typedef struct _ DATA_NODE
{
Int * pData;
Char * pFlag;
Int num;
} DATA_NODE;
# Define STATUS int
# Define TRUE 1
# Define FALSE 0 B) Create a memory Node
DATA_NODE * malloc_node (int number)
{
DATA_NODE * pDataNode = NULL;
If (0 = number)
Return NULL;
PDataNode = (DATA_NODE *) malloc (sizeof (DATA_NODE ));
Assert (NULL! = PDataNode );
Memset (pDataNode, 0, sizeof (DATA_NODE ));
PDataNode-> pData = (int *) malloc (sizeof (int) * number );
If (NULL = pDataNode-> pData ){
Free (pDataNode );
Return NULL;
}
PDataNode-> pFlag = (char *) malloc (number + 7)> 3 );
If (NULL = pDataNode-> pFlag ){
Free (pDataNode-> pData );
Free (pDataNode );
Return NULL;
}
Memset (pDataNode-> pData, 0, sizeof (int) * number );
Memset (pDataNode-> pFlag, 0, (number + 7)> 3 );
PDataNode-> num = number;
Return pDataNode;
}
DATA_NODE * malloc_node (int number)
{
DATA_NODE * pDataNode = NULL;
If (0 = number)
Return NULL;
PDataNode = (DATA_NODE *) malloc (sizeof (DATA_NODE ));
Assert (NULL! = PDataNode );
Memset (pDataNode, 0, sizeof (DATA_NODE ));
PDataNode-> pData = (int *) malloc (sizeof (int) * number );
If (NULL = pDataNode-> pData ){
Free (pDataNode );
Return NULL;
}
PDataNode-> pFlag = (char *) malloc (number + 7)> 3 );
If (NULL = pDataNode-> pFlag ){
Free (pDataNode-> pData );
Free (pDataNode );
Return NULL;
}
Memset (pDataNode-> pData, 0, sizeof (int) * number );
Memset (pDataNode-> pFlag, 0, (number + 7)> 3 );
PDataNode-> num = number;
Return pDataNode;
} C) delete a memory Node
STATUS free_node (const DATA_NODE * pDataNode)
{
If (NULL = pDataNode)
Return FALSE;
Assert (NULL! = PDataNode-> pData );
Assert (NULL! = PDataNode-> pFlag );
Assert (0! = PDataNode );
Free (pDataNode-> pFlag );
Free (pDataNode-> pData );
Free (void *) pDataNode );
Return TRUE;
}
STATUS free_node (const DATA_NODE * pDataNode)
{
If (NULL = pDataNode)
Return FALSE;
Assert (NULL! = PDataNode-> pData );
Assert (NULL! = PDataNode-> pFlag );
Assert (0! = PDataNode );
Free (pDataNode-> pFlag );
Free (pDataNode-> pData );
Free (void *) pDataNode );
Return TRUE;
} D) determine whether there is memory available for allocation
Int check_if_data_exist (const DATA_NODE * pDataNode)
{
Int number = pDataNode-> num;
Char * pFlag = pDataNode-> pFlag;
Unsigned char flag = 0;
Int loop = 1;
While (loop <= number ){
Flag = pFlag [(loop + 7)> 3-1] & (0x1 <(loop + 7) % 8 ));
If (0! = Flag ){
Return loop;
}
Loop ++;
}
Return-1;
}
Int check_if_data_exist (const DATA_NODE * pDataNode)
{
Int number = pDataNode-> num;
Char * pFlag = pDataNode-> pFlag;
Unsigned char flag = 0;
Int loop = 1;
While (loop <= number ){
Flag = pFlag [(loop + 7)> 3-1] & (0x1 <(loop + 7) % 8 ));
If (0! = Flag ){
Return loop;
}
Loop ++;
}
Return-1;
} E) allocate memory space
Int * alloca_data (const DATA_NODE * pDataNode)
{
Int * pData = NULL;
Int pos;
If (NULL = pDataNode)
Return NULL;
If (-1 = (pos = check_if_data_exist (pDataNode )))
Return NULL;
PDataNode-> pFlag [(pos + 7)> 3-1] | = 0x1 <(pos + 7) % 8 );
Return pDataNode-> pData + (pos-1 );
}
Int * alloca_data (const DATA_NODE * pDataNode)
{
Int * pData = NULL;
Int pos;
If (NULL = pDataNode)
Return NULL;
If (-1 = (pos = check_if_data_exist (pDataNode )))
Return NULL;
PDataNode-> pFlag [(pos + 7)> 3-1] | = 0x1 <(pos + 7) % 8 );
Return pDataNode-> pData + (pos-1 );
} F) reclaim memory space
STATUS free_data (const DATA_NODE * pDataNode, const int * pData)
{
Int pos = 0;
If (NULL = pDataNode | NULL = pData)
Return FALSE;
If (pData <pDataNode-> pData | pData> (pDataNode-> pData + pDataNode-> num ))
Return FALSE;
Pos = (pData-pDataNode-> pData)> 3;
PDataNode-> pFlag [(pos + 7)-1] & = ~ (0x1 <(pos + 7) % 8 ));
Return TRUE;
}
STATUS free_data (const DATA_NODE * pDataNode, const int * pData)
{
Int pos = 0;
If (NULL = pDataNode | NULL = pData)
Return FALSE;
If (pData <pDataNode-> pData | pData> (pDataNode-> pData + pDataNode-> num ))
Return FALSE;
Pos = (pData-pDataNode-> pData)> 3;
PDataNode-> pFlag [(pos + 7)-1] & = ~ (0x1 <(pos + 7) % 8 ));
Return TRUE;
} G) count the number of DWORD space allocated currently
Int count_free_space (const DATA_NODE * pDataNode)
{
Int count = 0;
Int loop = 1;
Char flag = 0;
If (NULL = pDataNode)
Return 0;
For (; loop <= pDataNode-> num; loop ++)
{
Flag = pDataNode-> pFlag [(loop + 7)> 3-1] & (0x1 <(loop + 7) % 8 ));
If (0 = flag ){
Count ++;
}
}
Return count;
}
Int count_free_space (const DATA_NODE * pDataNode)
{
Int count = 0;
Int loop = 1;
Char flag = 0;
If (NULL = pDataNode)
Return 0;
For (; loop <= pDataNode-> num; loop ++)
{
Flag = pDataNode-> pFlag [(loop + 7)> 3-1] & (0x1 <(loop + 7) % 8 ));
If (0 = flag ){
Count ++;
}
}
Return count;
}
The above code is just a demonstration. You can improve it on this basis, for example:
(1) modify the flag to allocate a lot of memory freely. Note that you need to modify the structure type of the flag at the same time.
(2) Change to a first-come-first-served memory allocation type.
(3) change to the memory allocation type of the most suitable space
(4) modify the configuration to debug-type memory allocation. During each allocation and release, check whether the memory is out of bounds and is not run in pairs. Note that you need to add the corresponding judgment function.