C Language static arraysandDynamic Array

C Arrays

In C language, there are two types of arrays:

  • Static arrays: memory is allocated at compile time, with a fixed size.
  • Dynamic array: 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, it is necessary to properly allocate and release memory to avoid memory leaks and accessing invalid memory.


Static Array

A static array is an array whose size is determined at compile time, and its size cannot be changed during program execution.

In C language, the memory of static arrays is allocated on the stack, usually using square brackets.[]to define.

Characteristics of static arrays include:

  • Memory allocation: The memory of static arrays is usually allocated on the stack, and is automatically managed as functions are called and return.
  • Fixed size: The size is specified at definition and cannot be changed during program execution.
  • Efficiency: Since memory is allocated on the stack, access speed is faster.
  • Lifetime: The lifetime of a static array begins at its definition. If it is defined inside a function, its lifetime is the same as the function invocation; if it is defined at global scope, its lifetime spans the entire program execution.

Example of declaring and initializing a static array:

int staticArray[5]; // 静态数组声明
int staticArray[] = {1, 2, 3, 4, 5}; // 静态数组声明并初始化
For static arrays, 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 above code, sizeof(array) returns the total number of bytes occupied by the entire array, while sizeof(array

The above is a simple static array example:

Example

#include <stdio.h>

int main() {
    int staticArray[] = {1, 2, 3, 4, 5}; // Static array declaration and initialization
    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 above example, we declared and initialized a static array `staticArray`, which contains 5 integer elements. Then, using the `sizeof` operator, we calculated the length of the static array and used a loop to traverse and print the elements of the array.

Output result:

静态数组: 1 2 3 4 5

Dynamic Array

A dynamic array is an array that, at runtime, through dynamic memory allocation functions (such asmallocandcalloc) manually allocates memory.

Dynamic arrays have the following characteristics:

  • Memory allocation: The memory space of a dynamic array is manually allocated at runtime through dynamic memory allocation functions and is stored on the heap. It is necessary to usemalloc、callocand other functions to allocate memory, and usefreefunction to free memory.
  • Example of declaration, memory allocation, and deallocation of a dynamic array:reallocUse a function to reallocate memory and change the size of the array.
  • Lifetime: The lifetime of a dynamic array is controlled by the programmer. Memory must be manually released after the array is no longer needed to avoid memory leaks.

The above is a simple example of using a dynamic array:

int size = 5;
int *dynamicArray = (int *)malloc(size * sizeof(int)); // 动态数组内存分配
// 使用动态数组
free(dynamicArray); // 动态数组内存释放

For a dynamically allocated array, you can store the array length when allocating memory dynamically and use that length when needed, for example:

int size = 5; // 数组长度
int *array = malloc(size * sizeof(int));

// 使用数组

free(array); // 释放内存

In the above code, we use the malloc function to dynamically allocate an integer array and store the length in the variable size. Then we can use this length as needed to perform operations, and after using the array, use the free function to release memory.

Note:When using dynamic arrays, attention must be paid to memory management, ensuring that memory is released when the array is no longer needed, to avoid memory leaks and accessing invalid memory locations.

In the above example, we first declared a variable `size` to specify the size of the dynamic array.

Example

#include <stdio.h>
#include <stdlib.h>

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 above example, we first declared a variable size to specify the size of the dynamic array.

Then use the malloc function to allocate memory for the dynamic array, and use the sizeof operator to calculate the required memory size.

Next, use a loop and the scanf function to read element values from user input and store them in the dynamic array.

Finally, use a loop to iterate through and print the elements of the dynamic array. At the end of the program, use the free function to release the memory occupied by the dynamic array.

Please note that when using dynamic arrays, it is necessary to check whether the memory allocation was successful (i.e., whether dynamicArray is NULL), to avoid errors when memory allocation fails.


C Arrays

other extensions