Thread overview | ||||||
---|---|---|---|---|---|---|
|
July 27, 2013 does pointer refer to a heap object? | ||||
---|---|---|---|---|
| ||||
Given a pointer to a struct, is there a clean way to determine if that struct is allocated on the heap or not? Or alternatively to determine whether it has finite or infinite lifetime (in TDPL speak)? |
July 27, 2013 Re: does pointer refer to a heap object? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Carl Sturtivant | On Saturday, 27 July 2013 at 17:58:20 UTC, Carl Sturtivant wrote:
>
> Given a pointer to a struct, is there a clean way to determine if that struct is allocated on the heap or not? Or alternatively to determine whether it has finite or infinite lifetime (in TDPL speak)?
import core.memory, std.stdio;
struct A { }
void main()
{
A a;
auto ap = &a;
auto bp = new A;
writeln("ap", GC.addrOf(ap) ? "" : " not", " allocated with the GC");
writeln("bp", GC.addrOf(bp) ? "" : " not", " allocated with the GC");
}
Note that the heap is more than just GC allocated memory. C malloc allocates on the heap but would fail the above test (as it should).
|
July 27, 2013 Re: does pointer refer to a heap object? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Brad Anderson | >
> import core.memory, std.stdio;
>
> struct A { }
>
> void main()
> {
> A a;
> auto ap = &a;
>
> auto bp = new A;
>
> writeln("ap", GC.addrOf(ap) ? "" : " not", " allocated with the GC");
> writeln("bp", GC.addrOf(bp) ? "" : " not", " allocated with the GC");
> }
>
>
> Note that the heap is more than just GC allocated memory. C malloc allocates on the heap but would fail the above test (as it should).
Nice, does what I need. Thanks.
|
July 27, 2013 Re: does pointer refer to a heap object? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Brad Anderson | Am 27.07.2013 20:07, schrieb Brad Anderson:
> On Saturday, 27 July 2013 at 17:58:20 UTC, Carl Sturtivant wrote:
>>
>> Given a pointer to a struct, is there a clean way to determine if that struct is allocated on the heap or not? Or alternatively to determine whether it has finite or infinite lifetime (in TDPL speak)?
>
> import core.memory, std.stdio;
>
> struct A { }
>
> void main()
> {
> A a;
> auto ap = &a;
>
> auto bp = new A;
>
> writeln("ap", GC.addrOf(ap) ? "" : " not", " allocated with the GC");
> writeln("bp", GC.addrOf(bp) ? "" : " not", " allocated with the GC");
> }
>
>
> Note that the heap is more than just GC allocated memory. C malloc allocates on the heap but would fail the above test (as it should).
core.thread has a function "getStackTop" and "getStackBottom", comparing pointers to the end of the stack (feteched like with the functions mentioned) should also work for malloc, but that's more of a hack.
PS: the not private functions are
thread_stackTop and thread_stackBottom
|
Copyright © 1999-2021 by the D Language Foundation