www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - convert interger to pointer

reply Moritz Warning <moritzwarning web.de> writes:
Hello,

I wrote a container that relies on treating two different values in a 
special way (they are used as some kind of flag).
For a container of integers, I just use 0 and 1 as special elements.
Those special entries are for internal use only.

The problem comes when I extend the container for reference types.
Instead of 0 I use null, but for 1 the trouble begins.

Is it possible to circumvent the type system and convert an integer to a 
pointer or class type? They won't ever be dereferenced, of course.

Many would probably think that this is the wrong way anyway,
but I try to avoid memory usage that way and to increase speed.

Introducing special workarounds also make the code more complicated.
Jun 28 2008
next sibling parent reply Moritz Warning <moritzwarning web.de> writes:
Ok, nvm. :)

void* p = cast(void*) 1;
A a = cast(A) p;
Jun 28 2008
next sibling parent reply Moritz Warning <moritzwarning web.de> writes:
On Sun, 29 Jun 2008 01:56:22 +0000, Moritz Warning wrote:

 Ok, nvm. :)
 
 void* p = cast(void*) 1;
 A a = cast(A) p;

ok, a problem remains. How to do this at compile time?: static const T first = cast(T) cast(void*) 0; static const T second = cast(T) cast(void*) 1; ..doesn't work.
Jun 28 2008
parent Jason House <jason.james.house gmail.com> writes:
Moritz Warning wrote:

 On Sun, 29 Jun 2008 01:56:22 +0000, Moritz Warning wrote:
 
 Ok, nvm. :)
 
 void* p = cast(void*) 1;
 A a = cast(A) p;

ok, a problem remains. How to do this at compile time?: static const T first = cast(T) cast(void*) 0; static const T second = cast(T) cast(void*) 1; ..doesn't work.

What about doing the reverse... at runtime cast to void* when doing the comparison. I think casting to void* is free
Jun 29 2008
prev sibling parent Moritz Warning <moritzwarning web.de> writes:
On Sun, 29 Jun 2008 18:27:18 -0400, Jason House wrote:

 Moritz Warning wrote:
 
 On Sun, 29 Jun 2008 01:56:22 +0000, Moritz Warning wrote:
 
 Ok, nvm. :)
 
 void* p = cast(void*) 1;
 A a = cast(A) p;

ok, a problem remains. How to do this at compile time?: static const T first = cast(T) cast(void*) 0; static const T second = cast(T) cast(void*) 1; ..doesn't work.

What about doing the reverse... at runtime cast to void* when doing the comparison. I think casting to void* is free

I did that already. Thanks for the suggestion.
Jun 29 2008
prev sibling parent reply Mike <vertex gmx.at> writes:
On Sun, 29 Jun 2008 03:35:03 +0200, Moritz Warning <moritzwarning web.de>  
wrote:

 Is it possible to circumvent the type system and convert an integer to a
 pointer or class type? They won't ever be dereferenced, of course.

union { int integer; void *pointer; } -- Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Jun 28 2008
parent Moritz Warning <moritzwarning web.de> writes:
On Sun, 29 Jun 2008 04:05:02 +0200, Mike wrote:

 On Sun, 29 Jun 2008 03:35:03 +0200, Moritz Warning
 <moritzwarning web.de> wrote:
 
 Is it possible to circumvent the type system and convert an integer to
 a pointer or class type? They won't ever be dereferenced, of course.

union { int integer; void *pointer; }

Would work probably. But I found another solution using a static and alias to fit in two base types quite nicely. (size_t and void*)
Jun 28 2008