[Issue 1778] Can not create pointer to class reference

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Jan 17 14:16:16 PST 2008


http://d.puremagic.com/issues/show_bug.cgi?id=1778





------- Comment #4 from aarti at interia.pl  2008-01-17 16:16 -------
Well, your solution is IMHO wrong for most real-life use cases. (And that was
my first try to solve problem).

BTW your example should look like below (there is an error in your's):
C c = new C;
C** ppc = new C*;
*ppc = &c;

And now, let's put this code into function which returns pointer to C:
---
import std.stdio;
class C {void test() {writefln("Ok");}}

C* func() {
    C c = new C;
    C** ppc = new C*;
    *ppc = &c;
    return *ppc;
}

void main() {func.test;}

---
Something that I don't understand in above code is the fact that it... works!
But it should definitely not!

Please notice in function func that there is address of local variable taken.
After exiting scope this address might be very quickly invalid. Without all
this pointer machinery, when you put just return &c; at the end of function you
get from compiler:

src/quicktest.d(7): Error: escaping reference to local variable c

IMHO it works just because of some specific stack access optimization in
my/others? computer. Or compiler makes some magic? 

I don't know why it works, but IMHO this bug should be reopened...


-- 



More information about the Digitalmars-d-bugs mailing list