0

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):

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)
LPs
  • 16,045
  • 8
  • 30
  • 61
Sam Autrey
  • 121
  • 1
  • 4
  • 7
    `temp = realloc((double*)ptr, counter*sizeof(double));` --> `temp = realloc(ptr, (counter+1)*sizeof(double));` – BLUEPIXY May 04 '17 at 05:51
  • 1
    [Please see this discussion on why not to cast the return value of `malloc()` and family in `C`.](http://stackoverflow.com/q/605845/2173917). – Sourav Ghosh May 04 '17 at 06:01

1 Answers1

2

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

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

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.

LPs
  • 16,045
  • 8
  • 30
  • 61