Leaving a pointer to it on the stack
Steven Schveighoffer
schveiguy at gmail.com
Thu Aug 13 20:11:50 UTC 2020
On 8/13/20 4:04 PM, Andre Pany wrote:
> Hi,
>
> in the specification
> https://dlang.org/spec/interfaceToC.html#storage_allocation there is
> this paragraph:
> "Leaving a pointer to it on the stack (as a parameter or automatic
> variable), as the garbage collector will scan the stack."
>
> I have some trouble to understand what does this mean. Given this example:
>
> ```
> import std;
>
> void main()
> {
> int* i;
> sample(&i);
> writeln(*i);
> }
>
> extern(C) export void sample(int** i)
> {
> *i = new int();
> **i = 42;
> }
> ```
>
> Int variable is created on the heap. How do I leave a pointer on the stack?
> (In the real coding, sample function will be called from Delphi)
>
The garbage collector scans all of the stack as if it were an array of
pointers. So if you have a pointer to it anywhere on the stack, it won't
be collected.
However, it only scans threads that the runtime knows about.
-Steve
More information about the Digitalmars-d-learn
mailing list