www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Expected behaviour, but can the error be detected?

reply "Lionello Lunesu" <lio lunesu.removethis.com> writes:
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;
}
Mar 25 2005
next sibling parent =?UTF-8?B?VGhvbWFzIEvDvGhuZQ==?= writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (MingW32)

iD8DBQFCRQ8X3w+/yD4P9tIRAguHAKCfJ57jhjZbxuG6H95kSm1TuOb0vACgmGeE
AbASHIjb9+BYyzrYlGVR6dE=
=Y8EI
-----END PGP SIGNATURE-----
Mar 25 2005
prev sibling parent "Walter" <newshound digitalmars.com> writes:
"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

 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.
May 08 2005