jeudi 4 mars 2021

Getting pointer from uintptr_t/intptr_t - how to check if the object is valid

I'm passing uintptr_t that represents result of new SomeClass; between threads. For obtaining the object I use

   void foo(uintptr_t n)
    {
      SomeClass *p = reinterpret_cast<SomeClass *>(n);
    }

It works fine, but I can't think of a wat to to check if p is a valid object, as if somehow foo will be called with value that doesn't correspont to a memory address of my SomeClass object I will get invalid pointer.

As far as I know using void* also have this issue.

Aucun commentaire:

Enregistrer un commentaire