[Issue 14033] runtime error about auto ptr = cast(T*)buf , T is class type

2015-01-23 Thread via Digitalmars-d-bugs
https://issues.dlang.org/show_bug.cgi?id=14033

--- Comment #3 from mzfh...@foxmail.com ---
i see,class and struct,their memory model is different,thanks.


struct stTest
{
int a;
int b;

void foo(){}
}
class clsTest
{
int a;
int b;

void foo(){}
}

void main()
{
int [100] buf  ;

auto stPtr = cast(stTest*)buf;
stPtr.a = 123;//ok
stPtr.foo();//ok

auto clsPtr = cast(clsTest)buf;
clsPtr.a = 123;//ok
clsPtr.foo();//run time error,vfptr is not be assigned.
}

--


[Issue 14033] runtime error about auto ptr = cast(T*)buf , T is class type

2015-01-23 Thread via Digitalmars-d-bugs
https://issues.dlang.org/show_bug.cgi?id=14033

Steven Schveighoffer schvei...@yahoo.com changed:

   What|Removed |Added

 CC||schvei...@yahoo.com

--- Comment #1 from Steven Schveighoffer schvei...@yahoo.com ---
I think both should be a compile-time error.

--


[Issue 14033] runtime error about auto ptr = cast(T*)buf , T is class type

2015-01-23 Thread via Digitalmars-d-bugs
https://issues.dlang.org/show_bug.cgi?id=14033

ag0ae...@gmail.com changed:

   What|Removed |Added

 Status|NEW |RESOLVED
 CC||ag0ae...@gmail.com
 Resolution|--- |INVALID

--- Comment #2 from ag0ae...@gmail.com ---
This is expected.

`clsPtr.a` does two dereferences:
1) `clsPtr` is dereferenced and yields a `clsTest` object. Class objects are
references (pointers) themselves. Since `buf` is all zeroes, you get a null
Object.
2) To access the `a` field, that null reference is then dereferenced. Boom,
segfault.

In code:
clsTest tmp = *clsPtr;
/* tmp is null */
tmp.a = 123; /* dereferencing null */

Closing as invalid.

--