View mode: basic / threaded / horizontal-split · Log in · Help
June 29, 2008
convert interger to pointer
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.
June 29, 2008
Re: convert interger to pointer
Ok, nvm. :)

void* p = cast(void*) 1;
A a = cast(A) p;
June 29, 2008
Re: convert interger to pointer
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/
June 29, 2008
Re: convert interger to pointer
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.
June 29, 2008
Re: convert interger to pointer
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*)
June 29, 2008
Re: convert interger to pointer
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
June 30, 2008
Re: convert interger to pointer
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.
Top | Discussion index | About this forum | D home