three little issues

bearophile bearophileHUGS at lycos.com
Sun Feb 6 05:13:21 PST 2011


spir:

> 2. reference escape
> 3. implicite deref

The situation is easy to understand once you know how generally what a stack frame is and how C functions are called:
http://en.wikipedia.org/wiki/Stack_frame
The D call stack is a contiguous-allocated backwards-single-linked list of differently-sized records, each record is a stack frame, and the whole data structure is of course managed as stack :-)

When you have similar doubts I also suggest you to take a look at the asm DMD generates. Writing asm requires some work, but reading a bit of asm is something you may learn in few days or even one day.

Before a D function starts, a stack frame is created. It will contain your stack-allocated struct instance. When the function ends its stack frame is destroyed virtually by moving a stack pointer, so the struct may be overwritten by other things, like by a call to writeln that creates many stack frames. If the stack frame is not overwritten and you save by *value* the stack contents, you have successfully saved your data in the array of S, but accessing virtually deleted data in the stack is a bad practice to avoid.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list