static array internal & dangling reference
    Picaud Vincent via Digitalmars-d-learn 
    digitalmars-d-learn at puremagic.com
       
    Sun Nov 13 21:53:04 PST 2016
    
    
  
On Sunday, 13 November 2016 at 23:39:37 UTC, Steven Schveighoffer 
wrote:
> Note that he is declaring an int[10] inside the function and 
> then returning it. The compiler must see that the int[10] will 
> be returned, and so it reuses the pre-allocated buffer for 
> returning as the same address to avoid copying.
>
> I would guess it's probably fine, with no dangling reference.
Thank you for your comment.
On my side there is still something mysterious. I one case:
int[] f()
{
   int[10] sa;
   foreach(int i, ref sa_i;sa){
     sa_i=i;
   }
   int[] sb=sa;
   return sb;
}
void f_test()
{
   auto sb=f();
   sb[2]=100;     // Valgrind ok
}
Valgrind does not complain.
But on the other case:
// same int[] f() function
void f_test() {
     auto sb=f();
     sb[2] = 100;
     writeln(sb[2]);
     int test[100];  // these two lines make Valgrind panicking
     writeln(sb[2]); //
}
it complains with "Conditional jump or move depends on 
uninitialised value(s)"
I think my two examples are creating a dangling pointer. But I 
would be happy to understand why Valgrind is not complaining for 
the first one.
Vincent
    
    
More information about the Digitalmars-d-learn
mailing list