Static and Dynamic Arrays in C
In C, there are two types of arrays:
- Static arrays: memory is allocated at compile time and the size is fixed.
- Dynamic arrays: memory is manually allocated at runtime and the size is variable.
The lifetime of a static array is related to its scope, while the lifetime of a dynamic array is controlled by the programmer.
When using dynamic arrays, you need to allocate and free memory properly to avoid memory leaks and invalid memory access.
Static arrays
A static array is an array whose size is determined at compile time and cannot change during program execution.
In C, memory for a static array is allocated on the stack, and it is usually defined using square brackets [].
The characteristics of static arrays include:
- Memory allocation: memory for a static array is usually allocated on the stack and managed automatically as functions are called and returned.
- Fixed size: the size is specified at definition and cannot be changed during program execution.
- Efficiency: because memory is allocated on the stack, access is fast.
- Lifetime: the lifetime of a static array begins at its definition. If defined inside a function, its lifetime is the same as the function call; if defined at global scope, its lifetime spans the entire program run.
Examples of declaring and initializing static arrays:
int staticArray[5]; // 静态数组声明
int staticArray[] = {1, 2, 3, 4, 5}; // 静态数组声明并初始化
For a static array, you can use the sizeof operator to get the array length, for example:
int array[] = {1, 2, 3, 4, 5};
int length = sizeof(array) / sizeof(array[0]);
In the code above, sizeof(array) returns the number of bytes occupied by the entire array, while sizeof(array[0]) returns the number of bytes of a single element; dividing the two gives the array length.
The following is a simple static array example:
Example
#include
int
main
(
)
{
int
staticArray
[
]
=
{
1
,
2
,
3
,
4
,
5
}
;
// Declare and initialize a static array
int
length
=
sizeof
(
staticArray
)
/
sizeof
(
staticArray
[
0
]
)
;
printf
(
"Static array: "
)
;
for
(
int
i
=
0
;
i
<
length
;
i
++
)
{
printf
(
"%d "
,
staticArray
[
i
]
)
;
}
printf
(
"\n"
)
;
return
0
;
}
In the example above, we declared and initialized a static array staticArray containing 5 integer elements. Then, using the sizeof operator, we calculated the length of the static array and used a loop to iterate over and print the elements.
Output result:
静态数组: 1 2 3 4 5
Dynamic array
A dynamic array is an array whose memory is manually allocated at runtime through dynamic memory allocation functions (such as malloc and calloc), an array whose memory is manually allocated.
The characteristics of dynamic arrays are as follows:
- Memory allocation: the memory space of a dynamic array is manually allocated at runtime through dynamic memory allocation functions and stored on the heap. You need to use
malloc,callocand other functions to request memory, and usefreefunction to free the memory. - Variable size: the size of a dynamic array can be adjusted at runtime as needed. You can use the
reallocfunction to reallocate memory and change the array size. - Lifetime: the lifetime of a dynamic array is controlled by the programmer. You need to manually free the memory after using the array to avoid memory leaks.
Examples of declaring, allocating memory for, and freeing dynamic arrays:
int size = 5;
int *dynamicArray = (int *)malloc(size * sizeof(int)); // 动态数组内存分配
// Using dynamic array
free(dynamicArray); // 动态数组内存释放
For a dynamically allocated array, you can save the array length when allocating memory and use that length when needed, for example:
int size = 5; // 数组长度
int *array = malloc(size * sizeof(int));
// Use array
free(array); // 释放内存
In the code above, we used the malloc function to dynamically allocate an integer array and stored the length in the variable size. Then we can use this length as needed, and after using the array, we use the free function to release the memory.
Note:Using dynamic arrays requires attention to memory management: make sure to free memory when the array is no longer needed, avoiding memory leaks and access to invalid memory locations.
The following is a simple dynamic array usage example:
Example
#include
#include
int
main
(
)
{
int
size
=
5
;
int
dynamicArray
=
(
int
)
malloc
(
size
sizeof
(
int
)
)
;
// Dynamic array memory allocation
if
(
dynamicArray
==
NULL
)
{
printf
(
"Memory allocation failed.\n"
)
;
return
1
;
}
printf
(
"Enter %d elements: "
,
size
)
;
for
(
int
i
=
0
;
i
<
size
;
i
++
)
{
scanf
(
"%d"
,
&
dynamicArray
[
i
]
)
;
}
printf
(
"Dynamic Array: "
)
;
for
(
int
i
=
0
;
i
<
size
;
i
++
)
{
printf
(
"%d "
,
dynamicArray
[
i
]
)
;
}
printf
(
"\n"
)
;
free
(
dynamicArray
)
;
// Dynamic array memory deallocation
return
0
;
}
In the example above, we first declared a variable size to specify the size of the dynamic array.
Then we used the malloc function to allocate memory for the dynamic array, and used the sizeof operator to calculate the required memory size.
Next, through a loop and the scanf function, we read element values from user input and store them in the dynamic array.
Finally, we use a loop to iterate over and print the elements of the dynamic array. At the end of the program, we use the free function to release the memory occupied by the dynamic array.
Note that when using dynamic arrays, you need to check whether memory allocation succeeded (that is, whether dynamicArray is NULL) to avoid errors when allocation fails.
C Arrays