小编wow*_*ofe的帖子

C中的free()2D数组使用malloc

我想使用free()从内存中删除整个矩阵数组.我该怎么做?

分配数组:

// test.h
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

#define BYTE unsigned char
#define  my_array(n_height, n_width) (BYTE **)create_array(sizeof(BYTE), (n_height), (n_width))

char  **create_array(int u_size, int height, int width);
char  **create_array(int u_size, int height, int width)
{
    char  **array;
    int     i;

    if (!(array=(char **)malloc(height*sizeof(char *)))) {
        printf("Memory allocation error.\n");
        exit(0);
    }
    if (!(array[0]=(char *)malloc(height*width*u_size))) {
        printf("Memory allocation error.\n");
        exit(0);
    }
    for (i=1; i<height; i++)
        array[i] = array[i-1] + width*u_size;
    return array;
}
Run Code Online (Sandbox Code Playgroud)
// test.c
#include "array.h"
int main()
{
    unsigned char …
Run Code Online (Sandbox Code Playgroud)

c arrays free matrix

3
推荐指数
1
解决办法
4959
查看次数

标签 统计

arrays ×1

c ×1

free ×1

matrix ×1