0

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

6
  • 4
    pc and temp end up pointing to the same address, so you must free only one of them. Actually, I think you could pc = realloc(pc,...) and do without temp altogether. Commented May 12, 2024 at 21:48
  • 2
    free(pc); is wrong. With int* temp = (int*)realloc(pc, sizeof(int) * 8); you've already given the pc pointer back to the heap system. Commented May 12, 2024 at 21:48
  • Note: Should I cast the result of malloc (in C)? Commented May 12, 2024 at 23:06
  • Where you have //Should i free(temp) here? -- (Answer: no). What would pc point to if you did? (hint: you just assigned the address held by temp to pc, so if you call free() on that address and then attempt to access pc - BOOM - invalid access after free....) Commented May 12, 2024 at 23:42
  • Thank you all for taking the time to answer, I wish i had more points to vote your answers. Commented May 13, 2024 at 0:42

2 Answers 2

1

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 );
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your answer you are also correct.I thought by freeing temp I was freeing the pointer from pointing in that location, but i realised what it does is freeing the block it points to.
If i free(temp) after the if statement I am loosing the allocated block right?The way this problem was solved was freeing either pc or temp after my calculations were over.
@Sotiris No. You can use either pointer after the if statement to free the memory because the both pointers store the same address of the allocated memory though it is better to use free( pc ); for readability because it is the main used pointer that was reassigned after realloc.
0

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

Remove free(temp).

2 Comments

Thank you very much, problem solved.So I never have to free temp?
If i free(temp) after the if statement I am loosing the allocated block right?

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.