C 中使用 malloc 的 free() 二维数组
我想使用 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;
}
// test.c
#include "array.h"
int main()
{
unsigned char *bytes;
BYTE **matrix;
matrix = my_array(height, width);
int c = 0;
for (int h=0; h < height; h++) {
for (int w=0; w < (width); w++) {
matrix[h][w] = bytes[c];
c++;
}
}
printf("Done.\n");
free(matrix); // really empty memory??
}
当我使用 free(matrix); 时,我不确定矩阵是否已从内存中完全删除。
I would like to use free() to remove the whole matrix arrays from memory. How can i do it?
Allocate arrays:
// 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;
}
// test.c
#include "array.h"
int main()
{
unsigned char *bytes;
BYTE **matrix;
matrix = my_array(height, width);
int c = 0;
for (int h=0; h < height; h++) {
for (int w=0; w < (width); w++) {
matrix[h][w] = bytes[c];
c++;
}
}
printf("Done.\n");
free(matrix); // really empty memory??
}
I am not sure whether matrix has been completely removed from the memory when i use free(matrix);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您想检查内存是否已释放,您应该使用
Valgrind
程序。请参阅Valgrind 快速入门指南
在这种情况下,请尝试以下操作:
If you want to check that the memory is freed, you should use the
Valgrind
program.See The Valgrind Quick Start Guide
In this case, try this:
每次调用
malloc()
时,您必须准确调用一次free()
。你不能“欺骗”free()
来 free:ing 几个块。如果您希望仅调用free()
一次,则需要确保在一次调用malloc()
中分配所有所需的内存。You must call
free()
exactly once per call tomalloc()
. You can't "fool"free()
into free:ing several blocks. If you want to be able to callfree()
only once, you'll need to make sure to allocate all the required memory in a single call tomalloc()
.您有两个
malloc
,因此需要两个free
。但是,如果您重新安排分配,则可以进行一些优化:这有两个优点。首先,可用性:您只需释放矩阵“对象”,而不必关心它是如何制作的。其次,效率:你拥有局部性并且只有一个分配,这都意味着速度。
You have two
malloc
s, so you need twofree
s. But you can optimise a bit if you rearrange your allocation:This has two advantages. First, usability: you only have to free your matrix "object" without bothering how it was made. Second, efficiency: you have locality AND only one allocation, which both means speed.
如果您愿意,可以使用漂亮的 C99 可变长度数组指针。
像这样分配:
索引像这样:
释放像这样:
If you like, you can use the beautiful C99 pointer-to-variable-length-array.
Allocate like this:
Index like this:
And free like this: