Sotiris
Sotiris

Reputation: 11

Problem using realloc() after calloc().Getting runtime error I do not know how to fix

I have tried solving this problem by googling about calloc() and realloc().

I saw videos about it.I read similar cases in stackoverflow but due to C++ and struct usage I had trouble understanding how to solve this problem.Since I am learning C and I am not yet familiar with the code that was provided in these examples.Also I have read about best practices on realloc() usage but still my problem is persistant.

So. I am learning C and about malloc(), calloc() and realloc() and although in vs2022 C++ 14 I get the desired results I also get :

Invalid address specified to RtlValidateHeap( 01150000, 01155D20 ) A breakpoint instruction (__debugbreak() statement or a similar call) was executed in filaname.cpp

Here is my code:

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




int main(int argc, char** argv)
{
    int* pm = (int*)malloc(sizeof(int) * 4);
    if (pm)
    {
        for (int i = 0; i < 4; i++)
        {
            *(pm + i) = i;
            printf("pm+i is : %p --- *pm+i is : %d\n", pm + i, *(pm + i));
        }
    }
    else
    {
        printf("malloc() failed.\n\n");
        return 1;
    }
    free(pm);
    printf("\n\n\n");

    int* pc = (int*)calloc(4, sizeof(int));
    if (pc)
    {
        for (int i = 0; i < 4; i++)
        {
            *(pc + i) = i * 2;
            printf("pc+i is : %p --- *pc+i is : %d\n", pc + i, *(pc + i));
        }
    }
    else
    {
        printf("calloc() failed.\n\n");
        return 1;
    }
    //free(pc); realloc() will free pc for me if needed .

    int* temp = (int*)realloc(pc, sizeof(int) * 8);
    if (temp)
    {
        pc = temp;
        //Should i free(temp) here?
        for (int i = 0; i < 8; i++)
        {
            *(pc + i) = i * 4;
            printf("pc+i is : %p --- *pc+i is : %d\n", pc + i, *(pc + i));
        }
    }
    else
    {
        printf("recalloc() failed.\n\n");
        return 1;
    
    }
    free(temp);
    free(pc);

    return 0;
}

This is my output as expected :

pm+i is : 01155D40 --- *pm+i is : 0
pm+i is : 01155D44 --- *pm+i is : 1
pm+i is : 01155D48 --- *pm+i is : 2
pm+i is : 01155D4C --- *pm+i is : 3



pc+i is : 01155D40 --- *pc+i is : 0
pc+i is : 01155D44 --- *pc+i is : 2
pc+i is : 01155D48 --- *pc+i is : 4
pc+i is : 01155D4C --- *pc+i is : 6
pc+i is : 01155D40 --- *pc+i is : 0
pc+i is : 01155D44 --- *pc+i is : 4
pc+i is : 01155D48 --- *pc+i is : 8
pc+i is : 01155D4C --- *pc+i is : 12
pc+i is : 01155D50 --- *pc+i is : 16
pc+i is : 01155D54 --- *pc+i is : 20
pc+i is : 01155D58 --- *pc+i is : 24
pc+i is : 01155D5C --- *pc+i is : 28

C:\Users\Strakizzz\Desktop\C\My Codeschool Projects\Pointers in C?C++\Debug\13_Dynamic memory allocation in C - malloc calloc realloc free.exe (process 368) exited with code -1.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .

When i get : "Invalid address specified to RtlValidateHeap( 01150000, 01155D20 ) A breakpoint instruction (__debugbreak() statement or a similar call) was executed in filaname.cpp" in debug mode, my program stops and loads symbols.My assumption is that i do not use free() properly but I am out of ideas what to do to fix this error that i get only at runtime.

Edit:I forgot to mention that a file called debug_heap.cpp opens after I close dissasembly window(which i have no idea how to read and also opens automatically).

Upvotes: 0

Views: 125

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 311088

The pointers temp and pc store the same address of the allocated memory due to this assignment statement

pc = temp;

So to free the memory use only one call of free instead of these two

free(temp);
free(pc);

as for example

free(pc);

Or it is better to free the allocated memory in the else part of the if statement and also after the if statement

if (temp)
{
    pc = temp;
    //Should i free(temp) here?
    for (int i = 0; i < 8; i++)
    {
        *(pc + i) = i * 4;
        printf("pc+i is : %p --- *pc+i is : %d\n", pc + i, *(pc + i));
    }
}
else
{
    printf("recalloc() failed.\n\n");
    free( pc );
    return 1;

}

free( pc );

Upvotes: 1

0___________
0___________

Reputation: 67835

You double free the same allocated memory as pc and temp reference the same memory location. It invokes UB

Remove free(temp).

Upvotes: 0

Related Questions