-1

I was reading C++ Is it possible to determine whether a pointer points to a valid object? and the correct answer in this thread is that no, you can't do that, but the thread is quite old now and I wanted to know if anything has changed. I read that with smart pointers that would be possible. So how could that be achieved?

r3k0j
  • 91
  • 6
  • You can keep a `std::set` of valid pointers, and use that to confirm that a given pointer is valid. – Eljay May 13 '22 at 16:02
  • 1
    Is your question "How do I know if a smart pointer (e.g. `std::shared_ptr` or `std::unique_ptr`) is pointing to a valid object?" Or is your question "Has anything changed with [this answer](https://stackoverflow.com/a/17202622/16287) about raw pointers?" – Drew Dormann May 13 '22 at 16:03
  • It's difficult to answer such a question because of its openness. By "pointer" do you really just mean a memory address, or a smart pointer object? What is "valid", and what kind of "object" do you mean. There are platform-dependent ways to determine if a memory address points to usable memory. – tenfour May 13 '22 at 16:04
  • my question is exactly the one in the link above. Given a pointer p, can you determine whether p points to a valid object? If so, how? If not, why not? – r3k0j May 13 '22 at 16:05
  • 1
    If you are talking about raw pointers, then the answer is still no. If you are talking about smart pointers, then as long as `get` does not return `nullptr` then you have a valid object as long as you didn't break the smart pointer contract – NathanOliver May 13 '22 at 16:06
  • not quite, but that also was helpful, thanks @AnoopRana – r3k0j May 13 '22 at 16:06
  • 1
    The answer to your linked question remains as valid as it was when posted. – Drew Dormann May 13 '22 at 16:06
  • If the pointer `p` is `nullptr`, then it does not point to a valid object. Otherwise, `p` either points to a valid object, or points to an arbitrary address (e.g., it is uninitialized), or it is a dangling pointer (points to a destructed object). – Eljay May 13 '22 at 16:07
  • On some embedded systems the address 0x0, is a valid address and contains data. However, some address lines may not be decoded or ranges may not be populated with readable or writable data. It's worse with operating systems because operating systems can place your code anywhere in memory and your program has no idea what that address is. Thus prefer pass by reference and use pointers only when necessary. – Thomas Matthews May 13 '22 at 16:13
  • [std::weak_ptr](https://en.cppreference.com/w/cpp/memory/weak_ptr) is maybe interesting to you. – Galik May 13 '22 at 16:14
  • both @DrewDormann – r3k0j May 13 '22 at 16:15
  • 1
    This may be of interest to you: https://stackoverflow.com/questions/12030650/when-is-stdweak-ptr-useful – Galik May 13 '22 at 16:18

1 Answers1

5

Is it possible to determine if a pointer points to a valid object

No, it isn't generally possible to determine whether a pointer points to a valid object.

I wanted to know if anything has changed

Nothing has changed in this regard.

I read that with smart pointers that would be possible. So how could that be achieved?

Unless you misuse it in a bad way, a smart pointer is never invalid. As such, there is no need to check whether it is valid or not. It should either be null or point to a valid object.

Weak pointer is a special kind of smart pointer that doesn't own an object directly, but it points to an object owned by shared pointer(s). The pointed object may be destroyed when no shared pointer points to it anymore. It's possible to ask the weak pointer whether that has happened or not. This state is somewhat analogous to being invalid, except the state is verifiable.

So how could that be achieved?

You can std::make_unique or std::make_shared to create a dynamic object owned by a smart pointer. The choice between them depends on what kind of ownership you need: unique or shared. Weak pointers can be created only from shared pointers.

eerorika
  • 232,697
  • 12
  • 197
  • 326