1

I have the following question.

If

int a = 5; 

and

int *b = new int(a); 

and then I do

delete b;

it will delete the object that b is pointing to if I am not mistaken. What I don't understand is what happens to b?

Does b remain a pointer pointing to the address where the object created by new used to live?

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
fibo11235
  • 153
  • 6

5 Answers5

3

then I do delete b; it will delete the object a that b is pointing to

It will delete the object that b is pointing to yes, but that object has nothing to do with the object a (other than they both have the same value).

What I dont understand is what happens to b? Does b remain a pointer pointing to the adress where the object created by new used to live.

That's exactly what happens to b i.e. nothing happens to it. delete only affects the dynamic memory management and pointed object (calling its destructor). The pointer variables value itself won't be changed, but is a dangling pointer afterwards.

eerorika
  • 232,697
  • 12
  • 197
  • 326
1

Yes, b remains a valid pointer - it's simply the address to which it points that becomes invalid. Declaring

int* b;

will reserve storage for a variable b (albeit unitialized), in just the same way as

int c;

reserves space for storing an integer. However, your b never pointed to the address of a - this is not what new int(a) does. Your b was a pointer to an address containing a copy of the value of a.

rwp
  • 1,786
  • 2
  • 17
  • 28
0

Your understanding is completely wrong. First, delete b; will call the destructor for the type e (a no-op in the case of a primitive type like int) but then it will crash because there will be an attempt to deallocate the object at b, but since this is an object with automatic storage duration the attempt will fail in a major way.

Also, the initialization of b would need to be "int * b = &a;", trying to assign an int to a int * will produce a compile-time error.

SoronelHaetir
  • 14,104
  • 1
  • 12
  • 23
0

When you delete an object by pointer, two things happen:

Once you call delete b you can reuse b to point to another object, like this

b = new int(123);

or like this

int a(123);
b = &a; // Obviously, you can't delete b after this

or you can set it to nullptr, like this

b = nullptr;

but you cannot dereference the pointer, because it would cause undefined behavior.

Does b remain a pointer pointing to the adress where the object created by new used to live?

Usually, that is exactly what happens - the value of b remains unchanged. However, there is no standard-compliant way to find out, because C++ implementation is free to set it in whichever way it likes. For example, the pointer could be set to a trap representation, so merely printing the value of the pointer would cause a crash.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

You can't delete memory that is not allocated by new. You can't apply deleteat any pointer you didn't initialize with new. delete b would exhibit undefined behavior, as the value of b was not obtained with new expression.

Thanks, Raghava