释放所有由 malloc(), realloc() 在 C 中分配的内存
Free all the memory allocated by malloc(), realloc() in C
我正在尝试通过 malloc()、realloc() 释放()所有分配的内存,但 valgrind 说这是内存泄漏。
代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
int lines_allocated = 128;
int max_line_len = 50;
FILE *fp;
/* File allocate lines of text */
char **array = (char **)malloc(sizeof(char*)*lines_allocated);
if (array==NULL) {
fprintf(stderr,"Out of memory (1).\n");
exit(1);
}
FILE *file = fopen("file.txt", "r");
if (file == NULL) {
fprintf(stderr,"Error opening file.\n");
exit(2);
}
int il;
for (il = 0; 1; il++) {
int j;
/* Have we gone over our line allocation? */
if (il >= lines_allocated) {
int new_size;
/* Double our allocation and re-allocate */
new_size = lines_allocated*2;
array = (char **)realloc(array,sizeof(char*)*new_size);
if (array==NULL) {
fprintf(stderr,"Out of memory.\n");
exit(3);
}
lines_allocated = new_size;
}
/* Allocate space for the next line */
array[il] = malloc(max_line_len);
if (array[il]==NULL) {
fprintf(stderr,"Out of memory (3).\n");
exit(4);
}
if (fgets(array[il], max_line_len-1, file)==NULL)
break;
/* Get rid of CR or LF at end of line */
for (j=strlen(array[il])-1;j>=0 && (array[il][j]=='\n' || array[il][j]=='\r');j--)
;
array[il][j+1]='[=10=]';
}
/* Close file */
fclose(file);
/* Print and free the every element of the array */
int cc;
for (cc = 0; cc < il; cc++) {
printf("%s\n", array[cc]);
/* Free the every element of the array */
free(array[cc]);
}
/* Free hole array */
free(array);
return 0;
}
valgrind ./main
valgrind --leak-check=full --show-reachable=yes ./main
==4806== Memcheck, a memory error detector
==4806== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==4806== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==4806== Command: ./main
==4806==
1
2
3
4
5
6
7
8
9
10
11
==4806==
==4806== HEAP SUMMARY:
==4806== in use at exit: 50 bytes in 1 blocks
==4806== total heap usage: 14 allocs, 13 frees, 2,192 bytes allocated
==4806==
==4806== 50 bytes in 1 blocks are definitely lost in loss record 1 of 1
==4806== at 0x4C2AC3D: malloc (vg_replace_malloc.c:299)
==4806== by 0x40092E: main (in /var/www/mem/main)
==4806==
==4806== LEAK SUMMARY:
==4806== definitely lost: 50 bytes in 1 blocks
==4806== indirectly lost: 0 bytes in 0 blocks
==4806== possibly lost: 0 bytes in 0 blocks
==4806== still reachable: 0 bytes in 0 blocks
==4806== suppressed: 0 bytes in 0 blocks
==4806==
==4806== For counts of detected and suppressed errors, rerun with: -v
==4806== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
如何正确释放内存?它说应该再释放一个内存块,但它在哪里?
for (cc = 0; cc < il; cc++) {
如果 il
是 array
的有效索引(确实如此),则循环中的比较应该是:
for (cc = 0; cc <= il; cc++) {
为了触发 array
的最后一个元素(并释放其内存)。
只需更换
for (cc = 0; cc < il; cc++)
和
for (cc = 0; cc <= il; cc++)
为了解决这个问题,想象一下如果分配循环 for (il = 0; 1; il++)
只迭代一次会发生什么。在这种情况下,控制不会到达 il++
,因此 il
保持为零并且 for (cc = 0; cc < il; cc++)
迭代零次。在一般情况下,释放循环比分配循环少一次迭代。
您的代码泄漏了最后一次分配,因为 il
在 fgets(array[il], max_line_len-1, file)
returns NULL
.
时永远不会递增
将 array[il] = malloc(max_line_len);
及其 NULL
检查移动到 fgets
之后将解决此问题。这种方法的另一个好处是您可以进行精确大小的分配,而不是在 max_line_len
.
处分配
// Check that we will need the allocation
char temp[max_line_len];
if (fgets(temp, max_line_len-1, file)==NULL) {
break;
}
// Allocate only when we are sure that we are going to need it
temp[max_line_len-1] = '[=10=]';
size_t len = strlen(temp);
array[il] = malloc(len+1);
if (array[il]==NULL) {
fprintf(stderr,"Out of memory (3).\n");
exit(4);
}
注意: 将 realloc
分配回正在重新分配的变量可能会导致泄漏先前分配给该变量的内存。这在您的代码中不是问题,因为您会立即调用 exit(4)
,但您应该了解此赋值的一般问题。
如果您遇到各种各样的问题,混合调用各种分配器,并且对事情非常挑剔,那么为各种内存分配器实现一个 wrapper(这可以是使用宏巧妙地完成)将新分配的缓冲区的地址缓存在某个地方(例如,在堆栈顶部)然后 - 在某个时候 - 导航堆栈并释放每个人。 不要 在这里和那里混合随机调用 free()
或其类似物。当某些东西被释放时,用零覆盖它,这样你就不会不小心尝试 free()
第二次,这会打乱 free()
.
如何巧妙地使用宏(我说了,所以我最好现在就让它工作)并避免递归问题:
让我们用malloc()
作为我们的第一个受害者。
在另一个源文件中,创建一个调用 malloc()
.
的函数 _malloc()
在包含所有内存分配和释放的源文件中,定义malloc()
如下:
#define malloc( n ) ( *sp++ = _malloc( n ) )
必须有代码作为序言调用,它设置堆栈并指向 sp
在它的底部。让它变得又好又大:你会 惊讶 有多少次 malloc()
和它的弟兄们可能最终被召唤。在不同的时间 — 在适当的时候,实际上⟩—⟩调用您自己的 free_all()
来执行此操作:
void free_all() {
while( --sp >= base_of_malloc_buf_stack ) {
free( *sp );
*sp = 0; /* avoid future re-free() */
}
}
我正在尝试通过 malloc()、realloc() 释放()所有分配的内存,但 valgrind 说这是内存泄漏。
代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
int lines_allocated = 128;
int max_line_len = 50;
FILE *fp;
/* File allocate lines of text */
char **array = (char **)malloc(sizeof(char*)*lines_allocated);
if (array==NULL) {
fprintf(stderr,"Out of memory (1).\n");
exit(1);
}
FILE *file = fopen("file.txt", "r");
if (file == NULL) {
fprintf(stderr,"Error opening file.\n");
exit(2);
}
int il;
for (il = 0; 1; il++) {
int j;
/* Have we gone over our line allocation? */
if (il >= lines_allocated) {
int new_size;
/* Double our allocation and re-allocate */
new_size = lines_allocated*2;
array = (char **)realloc(array,sizeof(char*)*new_size);
if (array==NULL) {
fprintf(stderr,"Out of memory.\n");
exit(3);
}
lines_allocated = new_size;
}
/* Allocate space for the next line */
array[il] = malloc(max_line_len);
if (array[il]==NULL) {
fprintf(stderr,"Out of memory (3).\n");
exit(4);
}
if (fgets(array[il], max_line_len-1, file)==NULL)
break;
/* Get rid of CR or LF at end of line */
for (j=strlen(array[il])-1;j>=0 && (array[il][j]=='\n' || array[il][j]=='\r');j--)
;
array[il][j+1]='[=10=]';
}
/* Close file */
fclose(file);
/* Print and free the every element of the array */
int cc;
for (cc = 0; cc < il; cc++) {
printf("%s\n", array[cc]);
/* Free the every element of the array */
free(array[cc]);
}
/* Free hole array */
free(array);
return 0;
}
valgrind ./main
valgrind --leak-check=full --show-reachable=yes ./main
==4806== Memcheck, a memory error detector
==4806== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==4806== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==4806== Command: ./main
==4806==
1
2
3
4
5
6
7
8
9
10
11
==4806==
==4806== HEAP SUMMARY:
==4806== in use at exit: 50 bytes in 1 blocks
==4806== total heap usage: 14 allocs, 13 frees, 2,192 bytes allocated
==4806==
==4806== 50 bytes in 1 blocks are definitely lost in loss record 1 of 1
==4806== at 0x4C2AC3D: malloc (vg_replace_malloc.c:299)
==4806== by 0x40092E: main (in /var/www/mem/main)
==4806==
==4806== LEAK SUMMARY:
==4806== definitely lost: 50 bytes in 1 blocks
==4806== indirectly lost: 0 bytes in 0 blocks
==4806== possibly lost: 0 bytes in 0 blocks
==4806== still reachable: 0 bytes in 0 blocks
==4806== suppressed: 0 bytes in 0 blocks
==4806==
==4806== For counts of detected and suppressed errors, rerun with: -v
==4806== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
如何正确释放内存?它说应该再释放一个内存块,但它在哪里?
for (cc = 0; cc < il; cc++) {
如果 il
是 array
的有效索引(确实如此),则循环中的比较应该是:
for (cc = 0; cc <= il; cc++) {
为了触发 array
的最后一个元素(并释放其内存)。
只需更换
for (cc = 0; cc < il; cc++)
和
for (cc = 0; cc <= il; cc++)
为了解决这个问题,想象一下如果分配循环 for (il = 0; 1; il++)
只迭代一次会发生什么。在这种情况下,控制不会到达 il++
,因此 il
保持为零并且 for (cc = 0; cc < il; cc++)
迭代零次。在一般情况下,释放循环比分配循环少一次迭代。
您的代码泄漏了最后一次分配,因为 il
在 fgets(array[il], max_line_len-1, file)
returns NULL
.
将 array[il] = malloc(max_line_len);
及其 NULL
检查移动到 fgets
之后将解决此问题。这种方法的另一个好处是您可以进行精确大小的分配,而不是在 max_line_len
.
// Check that we will need the allocation
char temp[max_line_len];
if (fgets(temp, max_line_len-1, file)==NULL) {
break;
}
// Allocate only when we are sure that we are going to need it
temp[max_line_len-1] = '[=10=]';
size_t len = strlen(temp);
array[il] = malloc(len+1);
if (array[il]==NULL) {
fprintf(stderr,"Out of memory (3).\n");
exit(4);
}
注意: 将 realloc
分配回正在重新分配的变量可能会导致泄漏先前分配给该变量的内存。这在您的代码中不是问题,因为您会立即调用 exit(4)
,但您应该了解此赋值的一般问题。
如果您遇到各种各样的问题,混合调用各种分配器,并且对事情非常挑剔,那么为各种内存分配器实现一个 wrapper(这可以是使用宏巧妙地完成)将新分配的缓冲区的地址缓存在某个地方(例如,在堆栈顶部)然后 - 在某个时候 - 导航堆栈并释放每个人。 不要 在这里和那里混合随机调用 free()
或其类似物。当某些东西被释放时,用零覆盖它,这样你就不会不小心尝试 free()
第二次,这会打乱 free()
.
如何巧妙地使用宏(我说了,所以我最好现在就让它工作)并避免递归问题:
让我们用malloc()
作为我们的第一个受害者。
在另一个源文件中,创建一个调用 malloc()
.
_malloc()
在包含所有内存分配和释放的源文件中,定义malloc()
如下:
#define malloc( n ) ( *sp++ = _malloc( n ) )
必须有代码作为序言调用,它设置堆栈并指向 sp
在它的底部。让它变得又好又大:你会 惊讶 有多少次 malloc()
和它的弟兄们可能最终被召唤。在不同的时间 — 在适当的时候,实际上⟩—⟩调用您自己的 free_all()
来执行此操作:
void free_all() {
while( --sp >= base_of_malloc_buf_stack ) {
free( *sp );
*sp = 0; /* avoid future re-free() */
}
}