Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

Two-Dimensional Array แบบ Runtime (Dynamic) ในภาษา C


Dynamic 2D Array คืออะไร?

การจองหน่วยความจำสำหรับ 2D Array **ระหว่างรันโปรแกรม (Runtime)** ใช้ malloc() หรือ calloc()

วิธีการจองหน่วยความจำ 2D Array แบบ Dynamic

วิธีที่ 1: Array of Pointers (จักรวาล Pointers)


int **arr = malloc(rows * sizeof(int *));
for (int i = 0; i < rows; i++)
    arr[i] = malloc(cols * sizeof(int));
  • แต่ละ Row อยู่คนละ Address (Scattered)
  • เข้าถึง: arr[i][j]
  • Free: ต้อง Free Row ทุก Row แล้วค่อย Free Array of Pointers

วิธีที่ 2: Contiguous Block (Single Malloc)


int *arr = malloc(rows * cols * sizeof(int));
// เข้าถึง: arr[i * cols + j]
  • หน่วยความจำติดกัน (Cache Friendly)
  • Free แค่ครั้งเดียว
  • เข้าถึงยุ่งยากกว่า

วิธีที่ 3: VLA (Variable Length Array) - C99


int rows, cols;
scanf("%d %d", &rows, &cols);
int arr[rows][cols]; // Stack allocation (จำกัดขนาด)

ตัวอย่างโปรแกรม: ผลรวมและผลคูณ 2D Array (ใช้ VLA)

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

int main() {
    int rows, cols;
    printf("Enter rows and cols: ");
    scanf("%d %d", &rows, &cols);
    
    // VLA (Stack) - ใช้ได้ถ้าขนาดไม่ใหญ่เกินไป
    int A[rows][cols], B[rows][cols];
    int sum[rows][cols], product[rows][cols];
    
    // Input Matrix A
    printf("Matrix A:\n");
    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++) {
            printf("A[%d][%d]: ", i, j);
            scanf("%d", &A[i][j]);
        }
    
    // Input Matrix B
    printf("Matrix B:\n");
    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++) {
            printf("B[%d][%d]: ", i, j);
            scanf("%d", &B[i][j]);
        }
    
    // Sum
    printf("\nSum Matrix:\n");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            sum[i][j] = A[i][j] + B[i][j];
            printf("%d\t", sum[i][j]);
        }
        printf("\n");
    }
    
    // Product
    printf("\nProduct Matrix:\n");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            product[i][j] = A[i][j] * B[i][j];
            printf("%d\t", product[i][j]);
        }
        printf("\n");
    }
    
    return 0;
}

ตัวอย่างแบบ Malloc (Heap) - แนะนำสำหรับ Array ใหญ่


#include 
#include 

int main() {
    int rows = 3, cols = 4;
    
    // Allocate
    int **A = malloc(rows * sizeof(int *));
    int **B = malloc(rows * sizeof(int *));
    int **sum = malloc(rows * sizeof(int *));
    
    for (int i = 0; i < rows; i++) {
        A[i] = malloc(cols * sizeof(int));
        B[i] = malloc(cols * sizeof(int));
        sum[i] = malloc(cols * sizeof(int));
    }
    
    // ... ใช้งาน A[i][j] ...
    
    // Free (ย้อนลำดับ)
    for (int i = 0; i < rows; i++) {
        free(A[i]); free(B[i]); free(sum[i]);
    }
    free(A); free(B); free(sum);
    
    return 0;
}

สรุปการเลือกใช้

วิธีเหมาะสำหรับข้อดีข้อเสีย
VLA (Stack)Array เล็ก-ปานกลาง ขนาดรู้ได้ตอน RunSyntax เรียบง่าย ใช้ [][] ได้Stack Overflow ถ้าใหญ่เกินไป
Array of Pointers (Heap)Array ใหญ่, จำเป็นต้อง Resize Rowยืดหยุ่น Free แยก Row ได้Memory Scattered, Free ซับซ้อน
Single Malloc (Heap)Array ใหญ่ ต้องการ PerformanceContiguous, Fast, Free ง่ายเข้าถึงแบบ Index Calculation