无效的free()/delete/delete[]/realloc()
这是我的代码:无效的free()/delete/delete[]/realloc(),c,malloc,free,realloc,C,Malloc,Free,Realloc,这是我的代码: #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;
#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;
}
有人知道为什么会发生这种情况吗?以下是代码中的一些问题,但不是所有问题:
\include
setvbuf(stdout,NULL,_IONBF,0)代码>和setvbuf(stderr,NULL,_IONBF,0)代码>除了使代码混乱之外没有任何效果
stTemp=malloc(st,sizeof(char)*(j+1))代码>
sizeof(char)
-Wall-Wextra-pedantic
我还使用:
-Wconversion-std=gnu99
您的分配都是错误的。它在windows上工作真是奇迹。首先,intx=1;int*numOfPhases=&x代码>有什么用??然后。。。你应该重写你的代码,因为你一直都有内存泄漏/分配太短。你读过编译警告了吗?哦,另一个:stTemp=malloc(st,sizeof(char)*(j+1))代码>和您从未使用过的stTemp
againI建议您阅读一篇文章。学会正确分配内存您只需为相位名称[i]
分配一个字符。您为stTemp
调用了一个char
数组,但从不使用或释放它st
指向与nameOfPhases[i]
相同的char
,当j
>0时,对st[j]
的访问无效。
==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)