Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无效的free()/delete/delete[]/realloc()_C_Malloc_Free_Realloc - Fatal编程技术网

无效的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;
}

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

以下是代码中的一些问题,但不是所有问题:

  • 'numOfPhases在初始化后不会更改,并且始终指向'x',因此可以通过始终使用'x'而不是'*numOfPhases'来简化

  • 当上述问题合并时,则可以消除变量“numOfPhases”

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

  • 发布的代码中没有使用“false”、“true”或“bool”,因此应删除语句
    \include

  • 所有输出语句都不缺少尾随'\n',因此语句
    setvbuf(stdout,NULL,_IONBF,0)
    setvbuf(stderr,NULL,_IONBF,0)除了使代码混乱之外没有任何效果

  • 变量“x”初始化为1,然后从未更改。建议使用“#define”而不是变量

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

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

  • 为了便于阅读和理解代码,请通过空行分隔代码块(for、if、else、while、do…while、switch、case、default)

  • 将任何值乘以1都没有任何区别,只会使代码变得混乱,因此不要乘以
    sizeof(char)

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

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

    对于“gcc”,至少使用:
    -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)