Special value for pointer (in addition to NULL)
-
Hello.
I need another one special value for some of my pointers. This value will mean "object was deleted, but some processing of dependent objects in needed". I cannot process dependent objects on-the-fly due to performance issues.
I tried
typedef T *pointer; pointer const DELETED = reinterpret_cast<pointer>(0xFFFFFFFF);, but got a warning from the compiler:
Warning C4312: 'reinterpret_cast' : conversion from 'unsigned int' to 'T *' of greater size. So I think this is unsave.Any suggestions?
Danke.
-
Here is what I have got from the help:
This warning detects 64-bit portability issues. You attempted to assign a 32-bit value to a 64-bit type. For example, casting a 32-bit int or 32-bit long to a 64-bit pointer.
This can be an unsafe conversion in some circumstances when sign extension occurs. If a negative number is assigned to a pointer type of a size greater than the int, sign extension will occur and the pointer value will refer to a memory address different from the value of the int.
-
SAn schrieb:
So I think this is unsave.
That probably doesn't make it unsafe - though alignment may be an issue. A better solution should be to use a distinct address not used otherwise. For example:
pointer const DELETED = static_cast<pointer>(std::malloc(sizeof(T)));After all, all that is needed is an address that is not be the same as any other object of that type, another object of any kind that has been allocated separately fits that bill.
-
Hm... That is interesting. Thank you.
Maybe this one will be faster because the adress will be known at compile time:
T const Object; typedef T *pointer; pointer const DELETED = &Object;??
-
SAn schrieb:
Hm... That is interesting. Thank you.
Maybe this one will be faster because the adress will be known at compile time:
T const Object; typedef T *pointer; pointer const DELETED = &Object;??
If your object can exist as a static entity that certainly is a possibility. I used malloc only because it can always be done that way regardless of what T is.
-
The need in additional value for pointers was a program architecture shortcoming. I managed to rewrite some parts of program to get rid of that.