Jump to page: 1 2 3
Thread overview
Validity of cast(void*)size_t.max
Feb 27, 2018
Nordlöw
Feb 27, 2018
Nordlöw
Feb 27, 2018
Ali Çehreli
Feb 28, 2018
Nordlöw
Feb 28, 2018
Kagamin
Feb 28, 2018
Ali Çehreli
Feb 28, 2018
Kagamin
Mar 02, 2018
Nordlöw
Mar 05, 2018
Nordlöw
Mar 05, 2018
Nordlöw
Mar 05, 2018
Nordlöw
Mar 05, 2018
Stefan Koch
Mar 06, 2018
Kagamin
February 27, 2018
Is `cast(void*)size_t.max` always an invalid address?

Is so, could it be used to indicate removed/delete elements in hash tables with open addressing and a key type being either a pointer or class?

And will it work correctly with the GC?
February 27, 2018
On 2/27/18 9:13 AM, Nordlöw wrote:
> Is `cast(void*)size_t.max` always an invalid address?

You mean, can it point at valid data? Possibly, but likely not. In my past as an embedded developer, a lot of times the interrupt vectors are stored at the end of address space.


> Is so, could it be used to indicate removed/delete elements in hash tables with open addressing and a key type being either a pointer or class?
>

Why not use null?

-Steve
February 27, 2018
On Tuesday, 27 February 2018 at 16:31:51 UTC, Steven Schveighoffer wrote:
> Why not use null?
>
> -Steve

It's already used to indicate that a slot is free. :)
February 27, 2018
On 2/27/18 11:37 AM, Nordlöw wrote:
> On Tuesday, 27 February 2018 at 16:31:51 UTC, Steven Schveighoffer wrote:
>> Why not use null?
>>
> 
> It's already used to indicate that a slot is free. :)

cast(void*)1 is likely to be unused.

-Steve
February 27, 2018
On 02/27/2018 11:56 AM, Steven Schveighoffer wrote:
> On 2/27/18 11:37 AM, Nordlöw wrote:
>> On Tuesday, 27 February 2018 at 16:31:51 UTC, Steven Schveighoffer wrote:
>>> Why not use null?
>>>
>>
>> It's already used to indicate that a slot is free. :)
> 
> cast(void*)1 is likely to be unused.
> 
> -Steve

And to be sure, one can have an actual object that represents nullness and use its pointer. (Similar to "the null object pattern".)

Ali
February 28, 2018
On Tuesday, 27 February 2018 at 20:14:01 UTC, Ali Çehreli wrote:
> And to be sure, one can have an actual object that represents nullness and use its pointer. (Similar to "the null object pattern".)
>
> Ali

Are there any pros to this pattern compared to just using `null` in D?
February 28, 2018
On Tuesday, 27 February 2018 at 19:56:44 UTC, Steven Schveighoffer wrote:
> cast(void*)1 is likely to be unused.

And since there was a word about class, cast(Class)cast(void*)1 won't compile :)
February 28, 2018
On Wednesday, 28 February 2018 at 19:22:12 UTC, Nordlöw wrote:
> Are there any pros to this pattern compared to just using `null` in D?

When null is already used to mean something else.
February 28, 2018
On 02/28/2018 11:22 AM, Nordlöw wrote:
> On Tuesday, 27 February 2018 at 20:14:01 UTC, Ali Çehreli wrote:
>> And to be sure, one can have an actual object that represents nullness and use its pointer. (Similar to "the null object pattern".)
>>
>> Ali
> 
> Are there any pros to this pattern compared to just using `null` in D?

Instead of checking explicitly against null later on, you simply use the object and it has no side-effects. I've probably never used it myself but it sounds nice if it's applicable. :)

Ali
February 28, 2018
On 2/28/18 3:00 PM, Kagamin wrote:
> On Tuesday, 27 February 2018 at 19:56:44 UTC, Steven Schveighoffer wrote:
>> cast(void*)1 is likely to be unused.
> 
> And since there was a word about class, cast(Class)cast(void*)1 won't compile :)

Oh, and it has a horrible message! Says you can't cast void * to Class. Which you can do, but apparently not if the compiler can tell if it's an integer you are casting from.

This does work:

auto x = cast(Object)((cast(size_t *)null) + 1);

I used size_t instead of ubyte, because it will avoid any weird bus errors on certain platforms. You'll just get a segfault.

-Steve
« First   ‹ Prev
1 2 3