digitalmars.D - convert interger to pointer
- Moritz Warning (12/12) Jun 28 2008 Hello,
- Moritz Warning (3/3) Jun 28 2008 Ok, nvm. :)
- Moritz Warning (6/10) Jun 28 2008 ok, a problem remains.
- Jason House (3/17) Jun 29 2008 What about doing the reverse... at runtime cast to void* when doing the
- Moritz Warning (3/22) Jun 29 2008 I did that already.
- Mike (9/11) Jun 28 2008 union
- Moritz Warning (4/15) Jun 28 2008 Would work probably.
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
Ok, nvm. :) void* p = cast(void*) 1; A a = cast(A) p;
Jun 28 2008
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
Moritz Warning wrote:On Sun, 29 Jun 2008 01:56:22 +0000, Moritz Warning wrote:What about doing the reverse... at runtime cast to void* when doing the comparison. I think casting to void* is freeOk, 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 29 2008
On Sun, 29 Jun 2008 18:27:18 -0400, Jason House wrote:Moritz Warning wrote:I did that already. Thanks for the suggestion.On Sun, 29 Jun 2008 01:56:22 +0000, Moritz Warning wrote:What about doing the reverse... at runtime cast to void* when doing the comparison. I think casting to void* is freeOk, 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 29 2008
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
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: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*)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; }
Jun 28 2008