提问人:NullPointer 提问时间:1/17/2022 更新时间:1/17/2022 访问量:528
C - 内存泄漏,函数返回字符*
C - Memory Leak, function return Char*
问:
对 C 编程进行刷新,我在释放内存时遇到了问题。下面的程序给了我下面的编译器警告,我很难解决。Valgrind 还通知存在内存泄漏,但我在使用 malloc() 分配的内存上使用 free。关于我在尝试在 main() 中的指针“alpha”上释放内存时做错了什么的任何指导是值得赞赏的?
编译器警告
In function ‘main’:/alphabetArrayPractice/src/main.c:37:10: warning: passing argument 1 of ‘free’ makes pointer from integer without a cast [-Wint-conversion]
37 | free(*alpha);
| ^~~~~~
| |
| char
In file included from /alphabetArrayPractice/src/main.c:2:
/usr/include/stdlib.h:565:25: note: expected ‘void *’ but argument is of type ‘char’
565 | extern void free (void *__ptr) __THROW;
Valgrind 报告
==950908==
==950908== HEAP SUMMARY:
==950908== in use at exit: 27 bytes in 1 blocks
==950908== total heap usage: 2 allocs, 1 frees, 1,051 bytes allocated
==950908==
==950908== 27 bytes in 1 blocks are definitely lost in loss record 1 of 1
==950908== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==950908== by 0x10919E: get_alphabet_array (main.c:5)
==950908== by 0x109230: main (main.c:23)
==950908==
==950908== LEAK SUMMARY:
==950908== definitely lost: 27 bytes in 1 blocks
==950908== indirectly lost: 0 bytes in 0 blocks
==950908== possibly lost: 0 bytes in 0 blocks
==950908== still reachable: 0 bytes in 0 blocks
==950908== suppressed: 0 bytes in 0 blocks
==950908==
==950908== For lists of detected and suppressed errors, rerun with: -s
==950908== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
法典
#include <stdio.h>
#include <stdlib.h>
char* get_alphabet_array(){
char *alpha = (char*) malloc(sizeof(char) * 27);
for(int i = 0; i < 27; i++) {
if(i == 0) {
alpha[i] = 'A';
} else if (i < 26) {
alpha[i] = alpha[i-1] + 1;
} else if (i == 26) {
alpha[i] = '\0';
break;
}
// printf("Character at index %d is: %c\n", i, alpha[i]);
}
return alpha;
}
int main () {
char* alpha = get_alphabet_array();
while(*alpha != '\0') {
static int count = 0;
printf("Letter at index %d: %c\n", count, *alpha);
count++;
*alpha++;
}
free(*alpha);
return 0;
}
答:
0赞
lurker
1/17/2022
#1
你从这个开始
char* alpha = get_alphabet_array();
while(*alpha != '\0') {
static int count = 0;
printf("Letter at index %d: %c\n", count, *alpha);
count++;
*alpha++;
}
free(*alpha); // <-- this attempts to free what `alpha` points to (not good)
它通过 ,试图释放指向的东西而不是指针。这或当然,没有意义。所以你把它改成了这个:free(*alpha)
alpha
alpha
char* alpha = get_alphabet_array();
while(*alpha != '\0') {
static int count = 0;
printf("Letter at index %d: %c\n", count, *alpha);
count++;
*alpha++; // <-- this alters the value of the pointer, `alpha`
}
free(alpha); // <-- this frees the *altered* value of `alpha` (not good)
现在,您遇到了一个不同的问题,因为自从您从内存分配中收到 的值以来,它已更改,并且正在释放错误的指针值。您需要释放原始指针。例如alpha
free(alpha)
char* alpha = get_alphabet_array();
for (char *p = alpha; *p != '\0'; p++) { // this loop does not alter `alpha`
static int count = 0;
printf("Letter at index %d: %c\n", count, *p);
count++;
}
free(alpha); // <-- this frees the originally allocated pointer
顺便说一句,该语句将指针递增到 ,然后取消引用指针并丢弃结果。所以这里并没有真正起到任何目的或作用。*alpha++;
alpha
*
评论
0赞
NullPointer
1/17/2022
谢谢!由于声誉得分低,我无法将其标记为解决方案,但非常感谢您的回应。
0赞
lurker
1/17/2022
@NullPointer很高兴它有所帮助!我相信你可以通过点击复选标记来“接受”答案,即使你不能给它投赞成票。
评论
free(alpha)
不。 是指针。 是它所指向的。 需要地址(指针)。free(*alpha)
alpha
*alpha
free
free(alpha)
alpha