gpt4 book ai didi

c - 无效的 free()/delete/delete[]/realloc()

转载 作者:行者123 更新时间:2023-11-30 21:00:45 28 4
gpt4 key购买 nike

这是我的代码:

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>

int main() {

setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);

int i=0;
int j=0;
int x=1;
int *numOfPhases=&x;
char** nameOfPhases=malloc((*numOfPhases)*sizeof(*nameOfPhases));
char* stTemp;
//TODO: check if NULL
for (i=0; i<(*numOfPhases);i++) {
nameOfPhases[i]=malloc(sizeof(char));
char *st=nameOfPhases[i];
printf("enter char\n");
do {
stTemp=malloc(st,sizeof(char)*(j+1));
//TODO: check if NULL
st[j]=getchar();
j++;
} while (st[j]!='\n');
if (j>=1) {
st[j-1]='\0';
}
st[j]=0;
printf("%s \n", nameOfPhases[i]);
j=0;
}

for (i=0;i<(*numOfPhases);i++) {
printf("%s ", nameOfPhases[i]);
}

for (i=0;i<(*numOfPhases);i++) {
free(nameOfPhases[i]);
}

free(nameOfPhases);
return 0;
}

当我在 Windows 上运行它时,一切正常。但是,当我通过 valgrind 在 Unix 上运行它时我遇到一些错误,例如:

==10215== Invalid read of size 1
==10215== at 0x343F047E2C: vfprintf (in /lib64/libc-2.12.so)
==10215== by 0x343F0495DF: buffered_vfprintf (in /lib64/libc-2.12.so)
==10215== by 0x343F04421D: vfprintf (in /lib64/libc-2.12.so)
==10215== by 0x343F04F189: printf (in /lib64/libc-2.12.so)
==10215== by 0x400837: main
==10215== Address 0x4c23090 is 0 bytes inside a block of size 1 free'd
==10215== at 0x4A06C20: realloc (vg_replace_malloc.c:662)
==10215== by 0x4007CC: main
==10215==
==10215== Invalid free() / delete / delete[] / realloc()
==10215== at 0x4A06430: free (vg_replace_malloc.c:446)
==10215== by 0x4008AF: main
==10215== Address 0x4c23090 is 0 bytes inside a block of size 1 free'd
==10215== at 0x4A06C20: realloc (vg_replace_malloc.c:662)
==10215== by 0x4007CC: main
==10215==
==10215==
==10215== HEAP SUMMARY:
==10215== in use at exit: 1 bytes in 1 blocks
==10215== total heap usage: 5 allocs, 5 frees, 12 bytes allocated
==10215==
==10215== 1 bytes in 1 blocks are definitely lost in loss record 1 of 1
==10215== at 0x4A06C20: realloc (vg_replace_malloc.c:662)
==10215== by 0x4007CC: main (in /homet2/ayeletk/OG/main.exe)
==10215==
==10215== LEAK SUMMARY:
==10215== definitely lost: 1 bytes in 1 blocks
==10215== indirectly lost: 0 bytes in 0 blocks
==10215== possibly lost: 0 bytes in 0 blocks
==10215== still reachable: 0 bytes in 0 blocks
==10215== suppressed: 0 bytes in 0 blocks
==10215==
==10215== For counts of detected and suppressed errors, rerun with: -v
==10215== ERROR SUMMARY: 4 errors from 3 contexts (suppressed: 6 from 6)

有人知道为什么会发生这种情况吗?

最佳答案

以下是代码中的一些问题,但不是全部:

  1. “numOfPhases 在初始化后永远不会改变,并且始终指向“x”,因此可以通过始终使用“x”而不是“*numOfPhases”来简化

  2. 当合并上述问题时,则可以消除变量'numOfPhases'

  3. “sizeof(char)”在标准中定义为 1,为清楚起见,使用 1

  4. 发布的代码中没有任何内容使用“false”、“true”或“bool”,因此语句 #include <stdbool.h>应该删除

  5. 所有输出语句均缺少尾随“\n”,因此语句 setvbuf(stdout, NULL, _IONBF, 0);setvbuf(stderr, NULL, _IONBF, 0);除了使代码变得困惑之外没有任何作用

  6. 变量“x”被初始化为 1,然后就不再改变。建议使用“#define”而不是变量

  7. 该语句无效,因为“malloc()”只有一个参数 stTemp=malloc(st,sizeof(char)*(j+1));

  8. 发布的代码中未定义数组“st[]”,因此对“st”的任何引用都会导致编译器输出错误消息

  9. 为了便于代码的可读性和理解,通过空行分隔代码块(for、if、else、while、do...while、switch、case、default)

    <
  10. 将任何值乘以 1 没有任何区别,只会使代码变得困惑,因此不要乘以 sizeof(char)

代码中还有很多其他问题,但这应该可以让您朝着正确的方向开始。

注意:始终启用所有编译器警告,然后修复这些警告。

对于“gcc”,至少使用:-Wall -Wextra -pedantic

我还使用:-Wconversion -std=gnu99

关于c - 无效的 free()/delete/delete[]/realloc(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38919452/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com