On Saturday, 5 April 2014 at 11:28:36 UTC, Jacob Carlborg wrote:
On 2014-04-04 15:25, "Marc Schütz" <schue...@gmx.net>" wrote:

This is unfortunately only true on x86 32-bit. For x86_64, the calling conventions (MS, SysV [1]) say that the first few parameters are passed in registers, and the same is probably true for other architectures.

I'm not so familiar with calling conventions and how the stack and registers work. But take this as an example:

extern (C) void foo (in char*);

void bar ()
{
    string s = "asd";
    foo(s.ptr);
}

Even if "s" is passed in a register to "foo", won't the stack of "bar" still be available until "foo" returns?

Yes, but it doesn't necessarily contain `s` anymore. Today's compilers are intelligent enough to see that `s` is never used after the function call, and therefore don't even allocate a stack slot for it.

`foo` could be implemented like this (it's a C function, so `in` boils down to `const` without `scope`):

char *b;
void foo (const char *a) {
    b = a;
// do something complex that causes all the registers to be reused // => the only reference to the string is now in b, outside of the GC's view
    // --> GC collects here <--
    printf(b); // the string may have been collected here
}

Reply via email to