0

I am new to C and I was trying to code up a dynamic int array that lets the user type in numbers via terminal untill the user types in -1. So the user can type any amount of int's and the data is reallocated each time another valid int is input.

Sometimes it works as expected other times I get: free(): double free detected in tcache 2 Aborted (core dumped) error. I am printing the values after every input and everything seems fine untill -1 is input. And the bug only occures every other time.

Here is my Code:

void output(int *ptr, int len){
    printf("\n");
    for(int i=0; i<len; i++){
        printf("%d %p \n", ptr[i], ptr+i);
    }

    printf("\n");
}

int input(int *ptr, int *size){

    int x;

    printf("Insert your numbers:\n");
    while(1){

        scanf("%d", &x);
        if(x == -1) break;

        size[0]++;
        // printf("Size: %d\n", size[0]);
        ptr = realloc(ptr, size[0] * sizeof(int));
        if(ptr == NULL) return -1;
        int indexToStore = size[0]-1;
        // printf("Index: %d\n", indexToStore);
        ptr[indexToStore] = x;
        output(ptr, size[0]);
    }
    return 0;
}


int main()
{
    int size = 0;
    int *array = (int*)malloc(sizeof(int));
    if(array == NULL) return -1;

    input(array, &size);
    output(array, size);
    input(array, &size);
    output(array, size);

    free(array); array = NULL;

    return 0;
}

2
  • regarding the statement int indexToStore = size[0]-1; on the first call to input() the value in size is 0, so size[0]-1 becomes 0xFFFFFFFF (-1) so indexToStore become -1. The result is undefined behavior in the statement: ptr[indexToStore] = x; Also, the function: output() is called twice for each call to input(), which is probably not what you want Commented May 15, 2021 at 15:53
  • every time the function: input() is called, another memory leak is created. This because the pointer array[] in main() is never updated, Commented May 15, 2021 at 15:59

1 Answer 1

1

The argument ptr of the function input is a copy of what is passed and modifying that will not affect what is passed in caller.

Therefore, the change in buffer via realloc() is not saved from the 1st call of input() to the 2nd call of input() and it will lead to troubles.

You should pass a pointer to what should be modified to have functions modify caller's local things.

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

void output(int *ptr, int len){
    printf("\n");
    for(int i=0; i<len; i++){
        printf("%d %p \n", ptr[i], ptr+i);
    }

    printf("\n");
}

int input(int **ptr, int *size){

    int x;

    printf("Insert your numbers:\n");
    while(1){

        scanf("%d", &x);
        if(x == -1) break;

        size[0]++;
        // printf("Size: %d\n", size[0]);
        *ptr = realloc(*ptr, size[0] * sizeof(int));
        if(*ptr == NULL) return -1;
        int indexToStore = size[0]-1;
        // printf("Index: %d\n", indexToStore);
        (*ptr)[indexToStore] = x;
        output(*ptr, size[0]);
    }
    return 0;
}


int main()
{
    int size = 0;
    int *array = (int*)malloc(sizeof(int));
    if(array == NULL) return -1;

    input(&array, &size);
    output(array, size);
    input(&array, &size);
    output(array, size);

    free(array); array = NULL;

    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.