在 C 語言中動態分配陣列

Jinku Hu 2023年1月30日 2021年2月28日
  1. 使用 malloc 函式在 C 語言中動態分配陣列
  2. 使用 realloc 函式修改 C 語言中已經分配的記憶體區域
  3. 使用巨集在 C 語言中實現給定物件的陣列分配
在 C 語言中動態分配陣列

本文將演示如何在 C 語言中動態分配陣列的多種方法。

使用 malloc 函式在 C 語言中動態分配陣列

malloc 函式是在堆上分配動態記憶體的核心函式。它分配給定的位元組數,並返回指向記憶體區域的指標。因此,如果要動態分配某些物件型別的陣列,首先應該宣告一個型別的指標。接下來,應該通過傳遞元素數量乘以單個物件的大小作為引數來呼叫 malloc

在下面的例子中,我們分配記憶體來儲存一個字元字串。按照安全編碼標準的要求,將 errno 設定為 0,並檢查 malloc 呼叫返回的指標,以驗證函式的成功執行。最後,利用 memmove 函式將字串複製到分配的記憶體位置。

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

#define SIZE 100

const char *str = "random string to be moved";

int main() {
    char *arr = NULL;

    errno = 0;
    arr = malloc(SIZE * sizeof(char));
    if (!arr) {
        perror("malloc");
        exit(EXIT_FAILURE);
    }

    memmove(arr, str, strlen(str));
    printf("arr: %s\n", arr);

    free(arr);
    exit(EXIT_SUCCESS);
}

輸出:

arr: random string to be moved

使用 realloc 函式修改 C 語言中已經分配的記憶體區域

realloc 函式用於修改之前由 malloc 呼叫分配的記憶體區域的大小。它將原始記憶體地址和新的大小作為第二個引數。注意,realloc 可能返回與傳遞的相同的指標,也可能根據請求的大小和給定地址後的可用記憶體返回不同的指標。同時,前一個陣列的內容將保持不變,直到新指定的大小。

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

#define SIZE 100

const char *str = "random string to be moved";

int main() {
    char *arr = NULL;

    errno = 0;
    arr = malloc(SIZE);
    if (!arr) {
        perror("malloc");
        exit(EXIT_FAILURE);
    }

    int num = 102;  // User Provided Value
    for (int i = 0; i < num; ++i) {
        if (i > SIZE) {
            arr = realloc(arr, 2 * SIZE);

            if (!arr) {
                perror("realloc");
                exit(EXIT_FAILURE);
            }
        }

        arr[i] = 'a';
    }

    free(arr);
    exit(EXIT_SUCCESS);
}

使用巨集在 C 語言中實現給定物件的陣列分配

通常,malloc 用於分配一些使用者定義結構的陣列。由於 malloc 返回的是 void 指標,並且可以隱式轉換為其他任何型別,所以比較好的做法是將返回的指標顯式轉換為對應的型別。因為它比較容易漏掉一些東西,並且不包含正確的標記,所以我們實現了一個巨集表示式,取陣列中的元素數和物件型別來自動構造正確的 malloc 語句,包括正確的強制轉換。

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

#define SIZE 100

typedef enum { Jan, Feb, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC} month;

typedef struct {
    unsigned char dd;
    month mm;
    unsigned yy;
} date;

#define MALLOC_ARRAY(number, type) \
    ((type *)malloc((number) * sizeof(type)))

int main() {
    date *d = NULL;

    errno = 0;
    d = MALLOC_ARRAY(SIZE, date);
    if (!d) {
        perror("malloc");
        exit(EXIT_FAILURE);
    }

    free(d);
    exit(EXIT_SUCCESS);
}
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn

相關文章 - C Array