K_inverse
K_inverse

Reputation: 367

Fail to delete a pointer

I am just learning pointer and dynamic memory allocation with keywords new and delete.

Below is my C++ code to test my understanding.

#include <iostream>
using namespace std;

int main() {

    // Variable to be pointed to by a pointer
    cout << "Create a double variable" << endl;
    double n = 3.1415926;
    cout << "n = " << n << endl;
    cout << "&n = " << &n << endl << endl;

    // Dynamic memory allocation
    cout << "Dynamically allocate memory to a pointer" << endl;
    double * ptr = new double;
    ptr = &n;       // Error when delete ptr
    // *ptr = n;    // Can delete ptr
    cout << "ptr = " << ptr << endl;
    cout << "*ptr = " << *ptr << endl << endl;


    // Free the pointer memory
    cout << "Delete the pointer" << endl;
    delete ptr;
    cout << "Done" << endl;

    return 0;
}

When the pointer ptr is pointing the the address of n (i.e. ptr = &n). The console print out is as follow with error.

$ ./test
Create a double variable
n = 3.14159
&n = 0x7ffee304f830

Dynamically allocate memory to a pointer
ptr = 0x7ffee304f830
*ptr = 3.14159

Delete the pointer
test(2436,0x118bf05c0) malloc: *** error for object 0x7ffee304f830: pointer being freed was not allocated
test(2436,0x118bf05c0) malloc: *** set a breakpoint in malloc_error_break to debug
Abort trap: 6

But when the pointer is assigned like this: *ptr = n. No such error occurs. Moreover, ptr is pointed to the address of 0x7faa7e400630 which is not the address of n

My understanding is that ptr in both cases is a valid pointer (with a valid address). But I don't know why delete ptr does not work for the case of ptr = &n. Any help to facilitate my understanding is appreciated. Thanks

Upvotes: 1

Views: 403

Answers (2)

eerorika
eerorika

Reputation: 238311

But I don't know why delete ptr does not work for the case of ptr = &n

n is an automatic variable. Deleting the address of an automatic variable has undefined behaviour.

delete expression on any pointer other than one that was returned by (non-placement) new expression has undefined behaviour (except a null pointer; it is safe to delete null). That is how the language is specified. When the behaviour is undefined, the program may produce an error.

ptr = &n assigns over the value that had been returned by new. Since the value is no longer stored anywhere, it will no longer be possible delete it. This is called a memory leak.

But when the pointer is assigned like this: *ptr = n. No such error occurs.

* is the indirection operator. That expression does not assign the pointer. ptr still points at the object in dynamic storage. It does not point to n. delete expression on a pointer was returned by a new-expression has well-defined behaviour. The behaviour is to destroy the object and to deallocate the memory.

This assignment sets the value of the object pointed by ptr. The value of that number is now the same value as n variable has.

Upvotes: 1

R Sahu
R Sahu

Reputation: 206567

But when the pointer is assigned like this: *ptr = n. No such error occurs.

When you execute *ptr = n, the pointer does not get deleted. It's a simple assignment operation. ptr points to a valid memory. Hence, assigning to *ptr is not a problem.

But I don't know why delete ptr does not work for the case of ptr = &n

You can only delete what has been obtained by new. &n is not one such pointer. Executing delete ptr is the same as executing delete &n. That is not correct and is cause for undefined behavior.

Upvotes: 2

Related Questions