Re: static array internal & dangling reference

2016-11-14 Thread Picaud Vincent via Digitalmars-d-learn
On Monday, 14 November 2016 at 17:15:43 UTC, Steven Schveighoffer wrote: What has happened is that the stack allocated for f() (and since released) is still referenced by sb[]. In a weird way, since you haven't called any other functions, that data is still "valid"! Thank you for the clarif

Re: static array internal & dangling reference

2016-11-14 Thread Steven Schveighoffer via Digitalmars-d-learn
On 11/14/16 12:53 AM, Picaud Vincent wrote: 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

Re: static array internal & dangling reference

2016-11-13 Thread Jonathan M Davis via Digitalmars-d-learn
On Monday, November 14, 2016 06:19:25 Picaud Vincent via Digitalmars-d-learn wrote: > On Monday, 14 November 2016 at 06:10:38 UTC, Jonathan M Davis > > wrote: > > I would have hoped that it would have complained about the > > first one. I don't know why it isn't. It definitely results in > > havin

Re: static array internal & dangling reference

2016-11-13 Thread Picaud Vincent via Digitalmars-d-learn
On Monday, 14 November 2016 at 06:10:38 UTC, Jonathan M Davis wrote: I would have hoped that it would have complained about the first one. I don't know why it isn't. It definitely results in having a pointer to memory that should no longer be referenced. Yes I would have hoped too, because

Re: static array internal & dangling reference

2016-11-13 Thread Jonathan M Davis via Digitalmars-d-learn
On Monday, November 14, 2016 05:53:04 Picaud Vincent via Digitalmars-d-learn wrote: > 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 > >

Re: static array internal & dangling reference

2016-11-13 Thread Picaud Vincent via Digitalmars-d-learn
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 avoi

Re: static array internal & dangling reference

2016-11-13 Thread Steven Schveighoffer via Digitalmars-d-learn
On 11/13/16 12:52 PM, cym13 wrote: On Saturday, 12 November 2016 at 12:16:02 UTC, Andrew wrote: On Saturday, 12 November 2016 at 11:03:31 UTC, Mike Parker wrote: [...] I thought that if you changed the function signature to int[10] f() to return a static array, this should be returned by valu

Re: static array internal & dangling reference

2016-11-13 Thread cym13 via Digitalmars-d-learn
On Saturday, 12 November 2016 at 12:16:02 UTC, Andrew wrote: On Saturday, 12 November 2016 at 11:03:31 UTC, Mike Parker wrote: [...] I thought that if you changed the function signature to int[10] f() to return a static array, this should be returned by value, and so should be safe to use, b

Re: static array internal & dangling reference

2016-11-12 Thread Mike Parker via Digitalmars-d-learn
On Saturday, 12 November 2016 at 13:11:02 UTC, Mike Parker wrote: void func2(int[] foo) { foo ~= 10; } Sorry, this should be (ref int[] foo)

Re: static array internal & dangling reference

2016-11-12 Thread Mike Parker via Digitalmars-d-learn
On Saturday, 12 November 2016 at 12:16:02 UTC, Andrew wrote: Bear in mind that static arrays are implicitly sliced when passed to or returned from a function anywhere a dynamic array is expected. If you modify f() to look like this: int[] f() { int[10] sa; foreach(int i, ref sa_i;sa){

Re: static array internal & dangling reference

2016-11-12 Thread Andrew via Digitalmars-d-learn
On Saturday, 12 November 2016 at 11:03:31 UTC, Mike Parker wrote: [...] You *have* created a dangling pointer. It's just that for such a simple little program, the part of the stack where the original array was allocated isn't stomped at the point where you access it after the function call.

Re: static array internal & dangling reference

2016-11-12 Thread Picaud Vincent via Digitalmars-d-learn
On Saturday, 12 November 2016 at 11:03:31 UTC, Mike Parker wrote: Thank you very much for your clarifications & explanations, I am reassured to see that things work like in C. I will also look the links you provided, thank you again for your time. Vincent

Re: static array internal & dangling reference

2016-11-12 Thread Picaud Vincent via Digitalmars-d-learn
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

Re: static array internal & dangling reference

2016-11-12 Thread Mike Parker via Digitalmars-d-learn
On Saturday, 12 November 2016 at 10:33:05 UTC, Picaud Vincent wrote: Hi all, Still learning... This time what surprised me is how static arrays work. I assume (is it true?) that for efficiency reason static size arrays like int[10] are on the stack and do not involve dynamic memory allocation:

Re: static array internal & dangling reference

2016-11-12 Thread cym13 via Digitalmars-d-learn
On Saturday, 12 November 2016 at 10:33:05 UTC, Picaud Vincent wrote: Hi all, Still learning... This time what surprised me is how static arrays work. I assume (is it true?) that for efficiency reason static size arrays like int[10] are on the stack and do not involve dynamic memory allocation:

static array internal & dangling reference

2016-11-12 Thread Picaud Vincent via Digitalmars-d-learn
Hi all, Still learning... This time what surprised me is how static arrays work. I assume (is it true?) that for efficiency reason static size arrays like int[10] are on the stack and do not involve dynamic memory allocation: First surprise: it is possible to share a static array: void main(