Open ghost opened 7 years ago
return r[i..];
This is undefined behavior, since r
is a local variable array. The slice you're returning includes a pointer to your local variable, which goes out of scope when you return, which means the pointer becomes invalid.
Now the question is, is there something zig could have done to catch this at runtime? Is this #211 ? Edit: oops, no it's not #211.
We have a compile error if you directly return an address of a local variable, using the runtime value hint system. I think it can be expanded to work for this situation.
@thejoshwolfe How to rewrite code to be correct?
How to rewrite code to be correct?
There are lots of places to put the array. The easiest one in my opinion is to make a local variable in the caller to right_encode
and pass in a slice for right_encode
to fill.
Or perhaps right_encode
is going to be a method on an object that can keep its own buffer as a field. The best answer to this question depends on what's really going on in your software.
@thejoshwolfe Thanks, I will play around.
if remove comment on line //%%io.stdout.printf("\n"); both mode works:
Uptodate environment: