Skip to content

Possibly incorrect answer to Exercise 2.23 #3

Open
@lisenkoevg

Description

@lisenkoevg

The answer to ex.2.23 says:

Exercise 2.23: Given a pointer p, can you determine whether p points to a valid
object. If so, how? If not, why not?

By Faisal saadatmand

Yes. Any of the following methods would work:

if (p != nullptr)  // preferred new C++ standard
if (p != 0)
if (p != NULL)     // requires cstdlib header
if (!p)
if (p)

I suggest, fact that pointer p is not equals to nullptr (or 0 or NULL) doesn't say anything about validity of object it points to. I suppose, it is not possible to determine whether p points to a valid object (and even whether it point to any object at all).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions