0

I have a simple problem like this!

class1 *a = new class1();
class1 *b = a;

delete a;
a= NULL;

Now I want to check if b is NULL (or is deleted) also, but b always point to where a point previously. This is problematic when I want to use b, but a already deleted before!

if (b){
    //do something here
}

Thanks for reading!

1

2 Answers 2

4

As some have suggested, using a shared pointer will make this easier.

If you want to do it the raw way, here is why *b still points to the original value of a

When you say class1 *b=a you are taking a copy of the pointer value of a. So regardless of what you do to a itself, b hangs on to this original value.

If you wanted it to change along with a, you would need to assign a reference to a, or a pointer to the a pointer

class1 **b = &a;

So now, think what happens when you dereference b, what value is that pointing to?

it will point to your original class1 *. So, once you set a=NULL, you now have a pointer b that points to the value of a, or NULL

Sign up to request clarification or add additional context in comments.

3 Comments

I think this is better than the shared pointer, as it preserves the flow of the original code. A shared pointer would alter the lifetime of the object to not match the original code.
this is true, though it requires more memory management than a shared pointer would require.
Yeah I think this approach work really well, but it will make the code look more Pointer-Way, a is already pointer, now b is a pointer of pointer ^^. Never try this before, but i think i will try to use it from now in some case! Thanks!
3

There is no way to have b update automatically after a has been NULLd. The question code also illustrates why setting a pointer to NULL after it's object has been deleted is partial, at best, and that a non-NULL pointer does not guarantee that a pointer is pointing to a valid object.

Use a std::shared_ptr<class1> instead, as there is shared ownership of the pointed-to object:

std::shared_ptr<class1> a = std::make_shared<class1>();
std::shared_ptr<class1> b = a;

the dynamically allocated object will be destructed with both a and b go out of scope.

See What C++ Smart Pointer Implementations are available? for an overview of available smart pointers.

5 Comments

so it's true we must use smart pointer in this case! No other solution for naked pointer !
@greenpig83 Yes, it is true. That's probably why smart pointers are created.
You don't HAVE to use smart pointers at all. See solution below. Some cases call for ease of use of smart pointers.
Shouldn't b be weak_ptr?
@Lol4t0, guess so. Depends really if it is essential that it exists for the other function, but the code suggests that is not the case so you are 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.