Thread overview | |||||
---|---|---|---|---|---|
|
March 25, 2005 Expected behaviour, but can the error be detected? | ||||
---|---|---|---|---|
| ||||
The following behaviour is entirely expected: the auto reference is saved in a global reference and is invalid after the object leaves the scope. I wonder if the compiler could have detect this 'error'. Prevent assigning an auto reference to another reference? Only allow assignment to variable within the scope? Just a thought. Lionello. // code class autoclass { public: void test() {} // will not get here }; static autoclass _b; void autotest() { auto autoclass _c = new autoclass; _b = _c; // this should be forbidden? } int main( char[][]arg ) { autotest(); // writefln("%08x",cast(int)cast(void*)_b); _b.test(); // "access violation" return 1; } |
March 26, 2005 Re: Expected behaviour, but can the error be detected? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lionello Lunesu Attachments: | Lionello Lunesu wrote: | The following behaviour is entirely expected: the auto reference | is saved in a global reference and is invalid after the object | leaves the scope. runs under Windows but segfault's on Linux ... | I wonder if the compiler could have detect this 'error'. Prevent | assigning an auto reference to another reference? Only allow | assignment to variable within the scope? Just a thought. | | Lionello. | | // code | class autoclass | { | public: | void test() {} // will not get here | }; | | static autoclass _b; | | void autotest() | { | auto autoclass _c = new autoclass; | _b = _c; // this should be forbidden? | } | | int main( char[][]arg ) | { | autotest(); | // writefln("%08x",cast(int)cast(void*)_b); | _b.test(); // "access violation" | return 1; | } Added to DStress as http://dstress.kuehne.cn/norun/auto_05.d Thomas |
May 09, 2005 Re: Expected behaviour, but can the error be detected? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lionello Lunesu | "Lionello Lunesu" <lio@lunesu.removethis.com> wrote in message news:d2175a$2u8h$1@digitaldaemon.com... > The following behaviour is entirely expected: the auto reference is saved in > a global reference and is invalid after the object leaves the scope. > > I wonder if the compiler could have detect this 'error'. Prevent assigning an auto reference to another reference? Only allow assignment to variable within the scope? Just a thought. >Added to DStress as http://dstress.kuehne.cn/norun/auto_05.d I'm hesitant to have the compiler detect this. There may be legitimate uses for it, such as instrumenting one's code for debugging purposes. For the time being, I'll say the usage is legitimate D, although dereferencing the reference will cause undefined behavior. |
Copyright © 1999-2021 by the D Language Foundation