stack frame & dangling pointer weirdness
Alain De Vos
devosalain at ymail.com
Thu Apr 21 11:25:29 UTC 2022
On Thursday, 21 April 2022 at 06:57:41 UTC, drug wrote:
> 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
> ```
No error was thrown during execution.
Should an error be thrown or is this optional by the operating
system ?
How can i force an error to be thrown when doing something "bad" ?
More information about the Digitalmars-d-learn
mailing list