stack frame & dangling pointer weirdness
drug
drug2004 at bk.ru
Thu Apr 21 06:57:41 UTC 2022
On 21.04.2022 08:49, Alain De Vos wrote:
> Following program:
> ```
> import std.stdio;
>
> void main() @trusted
> {
>
> int *p=null;
> void myfun(){
> int x=2;
> p=&x;
> writeln(p);
> writeln(x);
> }
> myfun();
> *p=16;
> writeln(p);
> writeln(*p);
> }
> ```
>
> outputs :
> 7FFFFFFFDFAC
> 2
> 7FFFFFFFDFAC
> 32767
>
> I don't understand why. Would it be possible to explain ?
Like others have said `writeln` overwrites the memory. You can check it
by commenting `writeln(p);` out - then you get:
```d
7FFF23725D1C
2
16
```
More information about the Digitalmars-d-learn
mailing list