Search code examples
cdynamic-memory-allocationrealloc

VS goes nuts after second realloc


So I have an input file, which has the following text (each line = user):

012345678;danny;cohen;22;M;danny1993;123;1,2,4,8;Nice person
223325222;or;dan;25;M;ordan10;1234;3,5,6,7;Singer and dancer
349950234;nadav;cohen;50;M;nd50;nadav;3,6,7,8;Engineer very smart

at first the code allocates room for one user, and then it reallocates room for 1 more (for each user). the thing is, that everything goes great till the second time reallocating, then it shows me the error "Exercise 6.exe has triggered a breakpoint." I will also mention the error was at first: "“wntdll.pdb not loaded” but I tried doing what the VS suggeset- something with symbols. Then came "triggered a breakpoint" error.

I tried switching the rows in the text file, but it didn't matter, the problem occurs after trying reallocating for the second time.

let me show you the relevant part of the code:

int main()
{
    //intiallizing variables
    int menArraySize = 0;
    //creating a NULL array pointer (array) for men
    User *head = NULL;
    readInputFile(head, &menArraySize, list);
}

void readInputFile(User *head, int *menArraySize, WomenList *list)
{
    //temporary data types for the the stracture we're creating
    char id[10];
    char *firstName = NULL;
    char *lastName = NULL;
    char age[4] = { 0 };
    char gender = '\0';
    char *userName = NULL;
    char *password = NULL;
    char hobbies = 0;
    char *description = NULL;
    //regular function data types
    int i = 0, j = 0, k = 0, currentDetail = 1, size;
    char currentString[212] = { 0 };
    int currentChar;

    //opening the input file
    FILE *input = fopen("input.txt", "r");
...
//long code for allocating room for each string (firstName, lastName, etc...)- irelevant
...
    head = addMale(head, menArraySize, id, firstName, lastName, age,
        gender, userName, password, hobbies, description);
...
//rest of the function isn't relevant
}

User* addMale(User *head ,int *menArraySize, char id[], char *firstName, 
char *lastName,char age[], char gender, char *userName,


char *password, char hobbies, char *description)
            {
                //if the array is empty, allocate room for one user
                if (*menArraySize == 0)
                {
                    head = (User *)malloc(1 * sizeof(User));
                }
                //if the array isn't empty, reallocate room for one more user
                else
                {
                    **this is the line in which the error occurs (second time reallocating,
                   third dynamic allocation total for this pointer)**
                head = (User *)realloc(head, (*menArraySize + 1) * sizeof(User));
                }
                //if the allocation failed
                if (head == NULL)
                    exit(1);
                //pointing to the new user we created
                head = &head[*menArraySize];
                //apply all details to the user
                strcpy(head->id, id);
                head->firstName = firstName;
                head->lastName = lastName;
                strcpy(head->age, age);
                head->gender = gender;
                head->userName = userName;
                head->password = password;
                head->hobbies = hobbies;
                head->description = description;
                //applying an index number to the user
                head->index = *menArraySize;
                //pointing back to the head of the array
                head = &head[0];
                //updating the variable showing the size of the array
                *menArraySize = *menArraySize + 1;
                return head;
            }

why is that happening? what can I do to fix it? thank you!


Solution

  • here:

    head = &head[*menArraySize];
    

    you're pointing on the new location, but you also overwrite (and lose) head original value (unless you substract). When you do:

    head = &head[0];
    

    you think you're restoring the original value, but that accomplishes nothing. You just reference/dereference the same value.

    Solution: use another User *temp value to reference the new location. And keep head unchanged after you used realloc.