Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
May 18, 2017 How to check a struct exists at a particular memory address? | ||||
---|---|---|---|---|
| ||||
This might be a really silly question but: I've allocated some memory like this (Foo is a struct): this._data = cast(Foo*) calloc(n, Foo.sizeof); How can I then later check that there is a valid Foo at `this._data` or `this._data + n`? |
May 18, 2017 Re: How to check a struct exists at a particular memory address? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | On Thursday, 18 May 2017 at 20:20:47 UTC, Gary Willoughby wrote:
> This might be a really silly question but:
>
> I've allocated some memory like this (Foo is a struct):
>
> this._data = cast(Foo*) calloc(n, Foo.sizeof);
>
> How can I then later check that there is a valid Foo at `this._data` or `this._data + n`?
Well... I think the right answer is that everything you do with memory should be very deterministic so you should just know where is what and not have a need to check :).
The only thing that crosses my mind if you really need to check is to make sure you always write some specific big number just before each struct in memory as a flag that what follows is Foo and then you can check if that is set properly. I think you could do this by wrapping Foo in another struct whose first field is immutable long set to some specific value (that isn't zero) :) and then using that struct in place of Foo. Although I am not sure if compiler would optimize away checks if an immutable is equal to its init value...
|
May 18, 2017 Re: How to check a struct exists at a particular memory address? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Igor | On Thursday, 18 May 2017 at 21:09:06 UTC, Igor wrote:
> On Thursday, 18 May 2017 at 20:20:47 UTC, Gary Willoughby wrote:
>> This might be a really silly question but:
>>
>> I've allocated some memory like this (Foo is a struct):
>>
>> this._data = cast(Foo*) calloc(n, Foo.sizeof);
>>
>> How can I then later check that there is a valid Foo at `this._data` or `this._data + n`?
>
> Well... I think the right answer is that everything you do with memory should be very deterministic so you should just know where is what and not have a need to check :).
Agreed. I think I'll re-visit the design.
|
May 19, 2017 Re: How to check a struct exists at a particular memory address? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | On Thursday, 18 May 2017 at 20:20:47 UTC, Gary Willoughby wrote:
> This might be a really silly question but:
>
> I've allocated some memory like this (Foo is a struct):
>
> this._data = cast(Foo*) calloc(n, Foo.sizeof);
>
> How can I then later check that there is a valid Foo at `this._data` or `this._data + n`?
The normal way to do this is to have an int (or whatever, larger size means less likely to fall fowl on random data) that is initialised to a known "random" value. See the ELF magic headers for example.
|
May 19, 2017 Re: How to check a struct exists at a particular memory address? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | On 5/18/17 4:20 PM, Gary Willoughby wrote:
> This might be a really silly question but:
>
> I've allocated some memory like this (Foo is a struct):
>
> this._data = cast(Foo*) calloc(n, Foo.sizeof);
>
> How can I then later check that there is a valid Foo at `this._data` or
> `this._data + n`?
The correct way to do it is to make _data a slice:
Foo[] _data;
...
this._data = (cast(Foo*)calloc(n, Foo.sizeof)[0 .. n];
Note, you should also initialize the data, as calloc does not. This might work:
_data[] = Foo.init;
-Steve
|
Copyright © 1999-2021 by the D Language Foundation