Can we check a pointer to make sure it is a valid address?

后端 未结 1 1435
无人及你
无人及你 2021-01-27 11:40

My idea is to print the object it points to. I think a valid pointer should have a valid object. If we try to print out the object we verify if the pointer is valid. Am I right?

相关标签:
1条回答
  • 2021-01-27 12:22

    I think a valid pointer should have a valid object.

    Yes, that's the definition of a valid pointer.

    If we try to print out the object we verify if the pointer is valid.

    Unfortunately, you can't. You can check whether the pointer is null; but if it wasn't initialised properly, or if it pointed to an object that's been destroyed, it will be neither valid nor null.

    If you want a pointer that's smart enough to know whether it's valid, you'll need a smart pointer.

    0 讨论(0)
提交回复
热议问题