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

时间:2016-08-12 13:41:39

标签: 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;
    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 个答案:

答案 0 :(得分:0)

这里有一些但不是代码中的所有问题:

  1. &#39; numOfPhases在初始化后永远不会更改,并始终指向&#39; x&#39;因此可以通过始终使用&#39; x&#39;来简化而不是&#39; * numOfPhases&#39;

  2. 当上述问题被纳入时,则可以消除变量&#39; numOfPhases&#39;

  3. &#39;的sizeof(char)的&#39;在标准中定义为1,为清楚起见使用1

  4. 发布的代码中没有任何内容使用&#39; false&#39;也不是真的&#39;也不是&#39; bool&#39;所以应删除声明#include <stdbool.h>

  5. 没有任何输出语句缺少尾随&#39; \ n&#39;所以语句setvbuf(stdout, NULL, _IONBF, 0);setvbuf(stderr, NULL, _IONBF, 0);没有任何效果,除了使代码混乱

  6. 变量&#39; x&#39;被初始化为1然后从未改变。建议使用&#39; #define&#39;而不是变量

  7. 此声明无效,因为&#39; malloc()&#39;只有一个参数stTemp=malloc(st,sizeof(char)*(j+1));

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

  9. 为了便于阅读和理解代码,单独的代码块(for,if,else,while,while,switch,case,default)通过空行

  10. 将任意值乘以1没有任何区别,只会使代码混乱,因此不要乘以sizeof(char)

  11. 代码中还有很多其他问题,但这应该让你开始朝着正确的方向前进。

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

    用于&#39; gcc&#39;,至少使用:-Wall -Wextra -pedantic

    我也使用:-Wconversion -std=gnu99