Difficulty understanding behavior of free()

Posted by Rasmi Ranjan Nayak on Stack Overflow See other posts from Stack Overflow or by Rasmi Ranjan Nayak
Published on 2012-09-06T09:09:53Z Indexed on 2012/09/06 9:38 UTC
Read the original article Hit count: 140

Filed under:
|
|
|
|
int main()
{
    int *ptr, **ptr1;
    ptr = (int*)malloc(sizeof(int));
    ptr1 = (int**)malloc(sizeof(int));
    free(ptr);
    *ptr = 12345;
    ptr1 = &ptr;

    //free(ptr);
    //**ptr1 = 23456;
    printf("%d \n", **ptr1);
    system("pause");
    return 0;
}

How does *ptr store the value 12345, when the memory has already been freed? So, now ptr should be pointing to garbage. Why is this happening?

© Stack Overflow or respective owner

Related posts about c++

Related posts about c