我正在编写一个动态分配的 C 程序
有人可以解释如何在“goto”中解决此内存泄漏
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
int main()
{
printf("start main\n");
char *f = NULL;
L1:
f = (char *)malloc(10);
if (f == NULL)
return -1;
printf("body lebel\n");
f = "A";
if (f == "B") {
free(f);
goto L1;
}
return 0;
return 0;
}
valgrind --leak-check=full ./a.out
,
输出:
==7217== HEAP SUMMARY:
==7217== in use at exit: 10 bytes in 1 blocks
==7217== total heap usage: 2 allocs, 1 frees, 1,034 bytes
allocated
第一的
f = "A";
将 char 指针分配f
给指向"A"
,它是const char*
存在于过程映像中某处的 。这完全孤立了您分配的内存malloc
。要将字符串复制到"A"
指向的内存中f
,您需要编写类似
strcpy(f, "A");
或者
strncpy(f, "A", 10);
有一个警告,strcpy
可能会超出您分配的缓冲区,f
并strncpy
可能使其非空终止。
其次,f
在跳转到L1
. 要f
在程序退出之前释放,您还必须free(f)
在return 0
.
第三,f == "B"
检查是否f
指向 const char* 值"B"
,而不是其内容是否与"B"
. 在您的程序中,这始终是错误的,因为您"A"
在比较之前将其分配给它。您需要strcmp
对空终止字符串或memcmp
可能的非空终止字符串使用类似的函数。
也可以将第一个移动free
到标签的开头,这样您就不必确保在跳转之前释放它,因为free(NULL)
它什么都不做。同样为了后代的利益,您应该将"A"
and声明"B"
为变量并将常量10
移到变量中,以帮助防止将来发生内存冲突:
int main()
{
const char a_str[] = "A";
const char b_str[] = "B";
size_t buffer_size = 10;
printf("start main\n");
char *f = NULL;
L1:
free(f);
f = (char *)malloc(buffer_size);
if (f == NULL)
return -1;
printf("body lebel\n");
if (buffer_size < sizeof(a_str)) {
printf("Buffer too small for %s\n", a_str);
exit(1);
}
memcpy(f, a_str, sizeof(a_str));
if (buffer_size < sizeof(b_str)) {
printf("Buffer too small for comparison with %s\n", b_str);
exit(1);
}
if (!memcmp(f, b_str, sizeof(b_str)) {
goto L1;
}
free(f);
return 0;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句