Closed SyrupThinker closed 4 years ago
I believe making it global also solves it.
The example will still fail in the way I describe, its just not stack data being printed, but that of the last greet call.
You could argue that thats intented behavior, but as an example for learning the language its in my opinion misleading.
https://github.com/ash/advent-2019/blob/c6e6b91a571651e71b412f1afd20e05a435050bf/Zig/function.zig#L5
This buffer is allocated on the stack, retuning a pointer (in form of a slice) to it is incorrect.
The way this code fails can be seen in the following example:
This problem can be solved by heap allocating the memory as mentioned in the blog post.