0

I am trying to allocate a dynamic array of Country objects for my school project. I have malloc'd the array in main() function and I am reallocating it in a add_country() function. but it seems to give me realloc invalid ponter error. Could someone help? This is the minimal reproducable code.

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    int count = 0;
    typedef struct test_Country
    {
    char name[20];
    int gold;
    int silver;
    int bronze;
    } test_Country;

    test_Country *addtest_Country(test_Country test_Country_obj, test_Country*array)
    {
    int flag = 0;
    printf("%s\n", "before realloc");
    test_Country *new_array;
    new_array = realloc(array, sizeof(test_Country *) * (count + 1));
    printf("%s\n", "after realloc");
    //array[count].name = (char *)malloc(strlen(test_Country_obj.name) + 1);
    if (count == 0)
    {
        strcpy(new_array[0].name, test_Country_obj.name);
    }
    else
    {

        for (int i = 0; i < count; i++)
        {

            if (strcasecmp(new_array[i].name, test_Country_obj.name) == 0)
            {
                printf("%s", "test_Country already added\n");
                flag = 1;
                break;
            }
        }
    }
    if (flag == 0)
    {
        strcpy(new_array[count].name, test_Country_obj.name);
        test_Country_obj.gold = 0;
        test_Country_obj.silver = 0;
        test_Country_obj.bronze = 0;
        new_array[count] = test_Country_obj;
        count = count + 1;
    }
    flag = 0;
    return new_array;
}

    int main()
    {
    char choice;
    test_Country *array = malloc(sizeof(test_Country));
    test_Country test_Country_obj;
    printf("%s", "Enter your choice : ");
    scanf("%s", &choice);
    //fgets(ptr, 80, stdin);
    //sscanf(ptr, "%c %s %d %d %d", &choice, test_Country_obj.name, &test_Country_obj.gold, &test_Country_obj.silver, &test_Country_obj.bronze);
    //printf("%s", &choice);
    while (choice != 'E')
    {

        printf("%s", "Enter test_Country name : ");
        scanf("%s", test_Country_obj.name);
        array = addtest_Country(test_Country_obj, array);
        //printf("%d%s", count, "is count");
        printf("%s", "Enter your choice : ");
        scanf("%s", &choice);
    }
    }

I cant seem to understand what is wrong.

3
  • it seems to give me realloc invalid ponter error. What is "it"? Please explain where that error is coming from and provide it verbatim in the post. Also, please provide complete code as a minimal reproducible example. Commented Apr 21, 2021 at 11:21
  • I am getting the same error. But I cant seem to understand what is causing it. I have put code causing the issue here. So when i choose 'A', the function goes to add_country() function. I am trying to increase the size of 'array' everytime a new country is added. The country_obj.name gets the name of Country as input. Commented Apr 21, 2021 at 11:39
  • I have added the minimal reproducible code. Commented Apr 21, 2021 at 12:06

1 Answer 1

2
char choice;
scanf("%s", &choice);

is bad. choice has only room for one character, so it can hold only strings upto zero characters. (the one-character room is for terminating null-character). Trying to store strings longer than zero character leads to dangerous out-of-range write and it may destroy data around that.

To avoid out-of-range write, you should allocate enough elements and specify the maximum length to read. The maximum length should be the buffer size minus one for terminating null-character.

char choice[16]; /* allocate enough elements */
scanf("%15s", choice); /* specify the maximum length */

After that, choice in the while and switch should be replaced with choice[0] to judge by the first character. Another way is using strcmp() to check the whole string.

Sign up to request clarification or add additional context in comments.

3 Comments

thank you for the suggestion, but I don't think this is causing the issue, because the program goes to the add_country function and at the realloc part and it breaks.
@av1028 Invoking undefined behavior doesn't always lead to crash.
I did not understand. Could you explain little more. I am new to programming. I have added the reproduceable code. Also, is it possible that it could be something wrong with my system?

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.