我想使用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)