pointer escaping return scope bug?
Dukc
ajieskola at gmail.com
Sat Nov 19 15:24:33 UTC 2022
On Saturday, 19 November 2022 at 15:02:54 UTC, Nick Treleaven
wrote:
> On Saturday, 19 November 2022 at 14:52:23 UTC, ag0aep6g wrote:
>> That's essentially just a function that returns its pointer
>> parameter. So the program boils down to this:
>>
>> ```D
>> @safe:
>> int* fp(return scope int* p) { return p; }
>> void main()
>> {
>> int* p;
>> {
>> auto lf = new int;
>> p = fp(lf);
>> }
>> assert(p != null); // address escaped
>> }
>> ```
>>
>> Which is fine, as far as I can tell. `lf` is not `scope`. And
>> when you pass it through `fp`, the result is still not
>> `scope`. So escaping it is allowed.
>>
>> You do get an error when you make `lf` `scope` (explicitly or
>> implicitly). So everything seems to be in order.
>
> OK, so how do I make `lf` implicitly scope?
Have the `int*` inside it to point to a local, or assign another
`scope int*` to it.
More information about the Digitalmars-d-learn
mailing list