简体   繁体   English

为什么realloc()和free()在我的代码中失败?

[英]Why does realloc() and free() fail in my code?

I have some problem with realloc() : 我对realloc()有一些问题:

int main(int argc, char* argv[])
{
    int* amis;
    int saisie, cpt = 1;

    while(saisie != -1) {
        printf("Entrer les notes -1 pour quitter :");
        scanf("%d", &saisie);
        if (cpt == 1) {
            amis = malloc(sizeof(int));
            if(amis == NULL) {
                printf("amis == NULL");
                exit(0);
            }
        }
        if(saisie != -1) {
           amis = realloc(amis, sizeof (int) + sizeof (amis));
           if(amis == NULL) {
                printf("amis == NULL       cpt= %d", cpt);
                exit(0);
            }
           amis[cpt] = saisie;
           printf("size = %d, saisie = %d, tab = %d \n", cpt * sizeof(int), saisie, amis[cpt]);
           cpt++;
        }
    }
    printf("%d",1==0);

    afficherTab(amis,cpt);
    printf("END\n");

    free(amis);
    return 0;
}

Why does realloc() cause an error when I use sizeof(int) * cpt instead of sizeof(amis) + sizeof(int) ? 当我使用sizeof(int) * cpt而不是sizeof(amis) + sizeof(int)时,为什么realloc()会导致错误?

free(amis) also doesn't work in that case. free(amis)在这种情况下也不起作用。

The biggest problem you have is that you seem to confuse a pointer with an array. 您遇到的最大问题是,您似乎将指针与数组混淆了。 If you use an array, then: 如果使用数组,则:

int foo[10];
printf("%zu\n", sizeof foo/ sizeof *foo);//sizeof foo/sizeof(int)

Will give you the length of the array, but a pointer is not an array. 会给你数组的长度,但是指针不是数组。 As I've explained here : 正如我在这里解释的

A pointer is not an array , so it doesn't need to know what the size of the array is. 指针不是数组 ,因此它不需要知道数组的大小。 A pointer can point to a single value, so a pointer can exist without there even being an array. 指针可以指向单个值,因此即使没有数组,指针也可以存在。 It doesn't even care where the memory it points to is situated (Read only, heap or stack... doesn't matter). 它甚至都不在乎它所指向的内存在哪里(只读,堆或堆栈...都没有关系)。 A pointer doesn't have a length other than itself. 指针的长度不等于自身。 A pointer just is... 指针就是...

So sizeof amis will always be the same value: the size of a memory address (4 on 32 bit, 8 on 64 bit). 因此sizeof amis将始终是相同的值:内存地址的大小(32位为4,64位为8)。 To address this, you're going to have to track the size of the allocated block yourself: 为了解决这个问题,您将不得不自己跟踪分配的块的大小:

size_t amis_size = 0;//use this

scanf(" %d",&saisie);//note the space before %d
amis_size += saisie;
amis = realloc(amis, sizeof *amis * amis_size);

And so on. 等等。
Other things you should do is: initialize your variables: 您还应该做的其他事情是:初始化变量:

int *amis = NULL,
    saisie = 0;

Fix the format of scanf , and check the value of saisie for negative values other than -1 ... 修复scanf的格式,并检查saisie的值saisie-1以外的负值...

Last but not least: exit(0); 最后但并非最不重要的一点: exit(0); means you're terminating the execution, with an exit status of 0. 0 means that the process terminated without error, whereas malloc or realloc failing is an error, use the stdlib macro exit( EXIT_FAILURE ); 表示您正在终止执行,退出状态为0。0表示进程已终止,没有错误,而mallocrealloc失败是错误,请使用stdlibexit( EXIT_FAILURE ); , or exit with a non-zero value. ,或以非零值退出。

Calling free on a pointer, just before the main function returns is a rather pointless thing to do, but calling free is a good habit to get into, so you can leave it there either way. 在主函数返回之前立即在指针上调用free是一件毫无意义的事情,但是调用free是一个很好的习惯,因此可以随意使用它。
However, try to get used to assigning NULL to any pointer you free: 但是,请尝试习惯于将NULL分配给您释放的任何指针:

free(amis);
amis = NULL;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM