static array internal & dangling reference

Picaud Vincent via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Nov 12 03:16:41 PST 2016


Thank you for your answer cym13.

I reproduced your result for:

On Saturday, 12 November 2016 at 10:45:23 UTC, cym13 wrote:
> void f_test() {
>     auto sb=f();
>     sb[2] = 100;
>     writeln(sb[2]); // prints 100
>     int test[100];
>     writeln(sb[2]); // prints 0
> }

now I am convinced of the invalid access, and this time valgrind 
was not happy, insulting me with:

==13545== Conditional jump or move depends on uninitialised 
value(s)
==13545==    at 0x10A6BA: 
_D3std4conv55__T11toTextRangeTiTS3std5stdio4File17LockingTextWriterZ11toTextRangeFNfiS3std5stdio4File17LockingTextWriterZv (indexType.d:5123)
==13545==    by 0x10ABED: 
_D3std5stdio4File14__T5writeTiTaZ5writeMFNfiaZv (stdio.d:1424)
==13545==    by 0x10A08C: 
_D3std5stdio14__T7writelnTiZ7writelnFNfiZv (stdio.d:3211)
....

However for my first example, I have checked again and

void f_test()
{
   auto sb=f();
   sb[2]=100;
}

Valgrind is silent... I thought it was because of a compiler 
optimization that removed the unusued "sb[2]=100" statement, but 
even with:


void f_test()
{
   auto sb=f();
   sb[2]=100;
   writeln(sb[2]);
}

which prints 100, Valgrind still do not complain... ?!?

I will try to investigate this and give a feedback if I find an 
explanation.

Vincent


More information about the Digitalmars-d-learn mailing list