Thread overview | |||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
February 27, 2018 Validity of cast(void*)size_t.max | ||||
---|---|---|---|---|
| ||||
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 Re: Validity of cast(void*)size_t.max | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | 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 Re: Validity of cast(void*)size_t.max | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | 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 Re: Validity of cast(void*)size_t.max | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | 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 Re: Validity of cast(void*)size_t.max | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | 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 Re: Validity of cast(void*)size_t.max | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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 Re: Validity of cast(void*)size_t.max | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | 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 Re: Validity of cast(void*)size_t.max | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | 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 Re: Validity of cast(void*)size_t.max | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | 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 Re: Validity of cast(void*)size_t.max | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kagamin | 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
|
Copyright © 1999-2021 by the D Language Foundation