Easy Tutorial
❮ C General Function Three Tier Architecture ❯

C Language Dynamic Arrays

Category Programming Techniques

In actual programming, it often occurs that the required memory space depends on the actual input data and cannot be predetermined. For such issues, it is difficult to solve with static arrays. To address the aforementioned problem, C language provides some memory management functions. These memory management functions, combined with pointers, can dynamically allocate memory space as needed to construct dynamic arrays. They can also reclaim unused space for reuse, providing a means for effective utilization of memory resources.

Dynamic arrays are in contrast to static arrays. The length of a static array is predefined and cannot be changed once the size is given throughout the program. However, dynamic arrays are different; they can be resized as needed by the program. The memory space of a dynamic array is allocated from the heap (dynamic allocation). It is allocated storage space by executing the code. The allocation is made when the program executes these statements. Programmers are responsible for releasing the memory themselves.

Why Use Dynamic Arrays?

In actual programming, it often occurs that the required memory space depends on the actual input data and cannot be predetermined. For such issues, it is difficult to solve with static arrays. To address the aforementioned problem, C language provides some memory management functions. These memory management functions, combined with pointers, can dynamically allocate memory space as needed to construct dynamic arrays. They can also reclaim unused space for reuse, providing a means for effective utilization of memory resources.

Comparison of Dynamic Arrays and Static Arrays

For static arrays, they are very convenient to create, and there is no need to release them after use. They are also simple to reference, but the fatal weakness is that their size cannot be changed after creation!

For dynamic arrays, they are more troublesome to create, and they must be released by the programmer themselves after use, otherwise, it can cause serious memory leaks. However, their use is very flexible, and they can dynamically allocate size according to the needs of the program.

How to Construct a Dynamic Array

Principles to Follow

When allocating, start from the outer layer and work your way inwards, allocating layer by layer;

When releasing, start from the inner layer and work your way outwards, releasing layer by layer.

Pointers Required for Construction

For constructing a one-dimensional dynamic array, a one-dimensional pointer is needed;

For two dimensions, a one-dimensional and a two-dimensional pointer is needed;

For three dimensions, one, two, and three-dimensional pointers are needed;

And so on.

Functions Required for Construction

Function Prototype Return Function Description
void *malloc(unsigned int size); Success: Returns the starting address of the allocated space Failure: Returns a null pointer Requests size bytes of heap space from the system
void *calloc(unsigned int num, unsigned int size); Success: Returns the starting address of the allocated space Failure: Returns a null pointer Applies for num size bytes of heap space by type
void free(void *p); No return value Releases the heap space pointed to by p
void *realloc(void *p,unsigned int size); Success: Returns the starting address of the newly allocated space Failure: Returns a null pointer Changes the heap space pointed to by p to size

Notes:

-

(1) It is defined as void * type, which does not mean that the function call has no return value, but rather returns the address of a node, which is of type void (no type or undetermined type), that is, the starting address of a storage area, the specific type of which cannot be determined and can only be determined when used according to the values of each field data. It can be converted to other types by forced conversion. For example: double *pd=NULL; pd=(double *)calloc(10,sizeof(double)); This means that 10 continuous double-type storage spaces are requested from the system, and the pointer pd points to the starting address of this continuous space. And (double) is used to convert the return type of calloc() so that the address of the double type data can be assigned to the pointer pd.

-

(2) The purpose of using sizeof is to calculate the number of bytes occupied by a type to suit different compilers.

(3) Since dynamic allocation may not be successful, an exception handling program should be added to prevent the program from stopping and leaving the user at a loss. A common exception handling program segment is: if(p==NULL) /* or if (!p) */ { printf("Dynamic memory allocation failed!\n"); exit(1); // Exceptional exit }

-

(4) The header files for these four functions are all included in

-

(5) The allocated heap space has no name and can only be found through the returned pointer.

-

(6) Never use free on non-

❮ C General Function Three Tier Architecture ❯