C realloc()错误中的动态数组

时间:2021-02-17 15:58:37

I am getting this error during runtime:

我在运行时遇到此错误:

*** Error in `./a.out': realloc(): invalid next size: 0x00000000020bd010 ***

I found other people getting this answer, however, I feel like mine is different. I am trying to allocate one extra space everytime the user specifies that he wants to enter another number. Any help is greatly appreciated! Thank you!

我发现其他人得到了这个答案,然而,我觉得我的不同。每当用户指定他想要输入另一个号码时,我试图分配一个额外的空间。任何帮助是极大的赞赏!谢谢!

Here is my code, and I'll link the output below.

这是我的代码,我将链接下面的输出。

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

int main()
{
        double *ptr = (double*)(malloc(sizeof(double)));
        double *temp;
        char answer = 'y';
        int counter = 1;

        printf("Enter in a double: ");
        scanf("%lf", ptr);

        printf("Your numer is %lf.\n", *ptr);

        printf("Do you want to enter another number? <y/n> : ");
        scanf(" %c", &answer);

        while(answer == 'y' || answer == 'Y')
        {
                temp = realloc((double*)ptr, counter*sizeof(double));
                ptr = temp;

                printf("Enter in a double: ");
                scanf("%lf", &ptr[counter]);

                printf("ptr[%d] = %lf.\n", counter, ptr[counter]);

                counter++;

                printf("Do you want to enter another number? <y/n> : ");
                scanf(" %c", &answer);
        }

        return 0;
}

Full Output(Note, this happens everytime I type in 4 numbers):

完全输出(注意,每次输入4个数字时都会发生这种情况):

Enter in a double: 123.123
Your numer is 123.123000.
Do you want to enter another number? <y/n> : y
Enter in a double: 123.321
ptr[1] = 123.321000.
Do you want to enter another number? <y/n> : y
Enter in a double: 456.321
ptr[2] = 456.321000.
Do you want to enter another number? <y/n> : y
Enter in a double: 453.23456
ptr[3] = 453.234560.
Do you want to enter another number? <y/n> : y
*** Error in `./a.out': realloc(): invalid next size: 0x0000000000c52010 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7f64da1777e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x82a5a)[0x7f64da182a5a]
/lib/x86_64-linux-gnu/libc.so.6(realloc+0x179)[0x7f64da183c89]
./a.out[0x400750]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f64da120830]
./a.out[0x4005c9]
======= Memory map: ========
00400000-00401000 r-xp 00000000 08:05 6183060                            /home/sam/Documents/compsci/sja0128/Labs/LabEC/a.out
00600000-00601000 r--p 00000000 08:05 6183060                            /home/sam/Documents/compsci/sja0128/Labs/LabEC/a.out
00601000-00602000 rw-p 00001000 08:05 6183060                            /home/sam/Documents/compsci/sja0128/Labs/LabEC/a.out
00c52000-00c73000 rw-p 00000000 00:00 0                                  [heap]
7f64d4000000-7f64d4021000 rw-p 00000000 00:00 0 
7f64d4021000-7f64d8000000 ---p 00000000 00:00 0 
7f64d9ee8000-7f64d9efe000 r-xp 00000000 08:05 1709130                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7f64d9efe000-7f64da0fd000 ---p 00016000 08:05 1709130                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7f64da0fd000-7f64da0fe000 rw-p 00015000 08:05 1709130                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7f64da100000-7f64da2bf000 r-xp 00000000 08:05 1726267                    /lib/x86_64-linux-gnu/libc-2.23.so
7f64da2bf000-7f64da4bf000 ---p 001bf000 08:05 1726267                    /lib/x86_64-linux-gnu/libc-2.23.so
7f64da4bf000-7f64da4c3000 r--p 001bf000 08:05 1726267                    /lib/x86_64-linux-gnu/libc-2.23.so
7f64da4c3000-7f64da4c5000 rw-p 001c3000 08:05 1726267                    /lib/x86_64-linux-gnu/libc-2.23.so
7f64da4c5000-7f64da4c9000 rw-p 00000000 00:00 0 
7f64da4d0000-7f64da4f6000 r-xp 00000000 08:05 1726269                    /lib/x86_64-linux-gnu/ld-2.23.so
7f64da6ef000-7f64da6f5000 rw-p 00000000 00:00 0 
7f64da6f5000-7f64da6f6000 r--p 00025000 08:05 1726269                    /lib/x86_64-linux-gnu/ld-2.23.so
7f64da6f6000-7f64da6f7000 rw-p 00026000 08:05 1726269                    /lib/x86_64-linux-gnu/ld-2.23.so
7f64da6f7000-7f64da6f8000 rw-p 00000000 00:00 0 
7fff1d240000-7fff1d261000 rw-p 00000000 00:00 0                          [stack]
7fff1d270000-7fff1d272000 r--p 00000000 00:00 0                          [vvar]
7fff1d272000-7fff1d274000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Aborted (core dumped)

1 个解决方案

#1


2  

Your code has a main problem: arrays index start from 0 to size-1 using . So using

您的代码有一个主要问题:数组索引使用c从0开始到size-1。所以使用

temp = realloc((double*)ptr, counter*sizeof(double));

in first loop you are allocating a single item (counter=1) and after that you access ptr[counter] while max index can be counter-1

在第一个循环中,您正在分配单个项目(counter = 1),之后您访问ptr [counter],而max index可以是counter-1

Moreover the code before the loop is the same inside the loop, so you can avoid it:

而且循环之前的代码在循环内是相同的,所以你可以避免它:

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

int main(void)
{
    double *ptr = NULL;
    double *temp;
    char answer = 'n';
    int counter = 0;

    do
    {
        temp = realloc(ptr, (counter+1) * sizeof(double));

        if (temp != NULL)
        {
            ptr = temp;

            printf("Enter in a double: ");
            scanf("%lf", &ptr[counter]);

            printf("ptr[%d] = %lf.\n", counter, ptr[counter]);

            printf("Do you want to enter another number? <y/n> : ");
            scanf(" %c", &answer);

            counter++;
        }
    }
    while (((answer == 'y') || (answer == 'Y')) && (temp != NULL));

    free(ptr);

    return 0;
}

As you can see all realloc returned value must be checked before used.

如您所见,在使用之前必须检查所有realloc返回值。

#1


2  

Your code has a main problem: arrays index start from 0 to size-1 using . So using

您的代码有一个主要问题:数组索引使用c从0开始到size-1。所以使用

temp = realloc((double*)ptr, counter*sizeof(double));

in first loop you are allocating a single item (counter=1) and after that you access ptr[counter] while max index can be counter-1

在第一个循环中,您正在分配单个项目(counter = 1),之后您访问ptr [counter],而max index可以是counter-1

Moreover the code before the loop is the same inside the loop, so you can avoid it:

而且循环之前的代码在循环内是相同的,所以你可以避免它:

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

int main(void)
{
    double *ptr = NULL;
    double *temp;
    char answer = 'n';
    int counter = 0;

    do
    {
        temp = realloc(ptr, (counter+1) * sizeof(double));

        if (temp != NULL)
        {
            ptr = temp;

            printf("Enter in a double: ");
            scanf("%lf", &ptr[counter]);

            printf("ptr[%d] = %lf.\n", counter, ptr[counter]);

            printf("Do you want to enter another number? <y/n> : ");
            scanf(" %c", &answer);

            counter++;
        }
    }
    while (((answer == 'y') || (answer == 'Y')) && (temp != NULL));

    free(ptr);

    return 0;
}

As you can see all realloc returned value must be checked before used.

如您所见,在使用之前必须检查所有realloc返回值。