static array internal & dangling reference
Steven Schveighoffer via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Nov 13 15:39:37 PST 2016
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 value, and so should
>> be safe to use, but it seems that this too points to the same memory.
>
> This is likely because your examples are too simple and the local
> variables of each stack frame happen to be at the same place with the
> same value. There's nothing really conclusive here.
No, it's some sort of compiler optimization.
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.
-Steve
More information about the Digitalmars-d-learn
mailing list