Boolean (or int) returned by a function that allocates memory on the heap #10
Replies: 3 comments 3 replies
-
|
If you're talking about detecting "alloc-without-free" situations at compile time, that's not possible. As for allocation functions, there's mem.TryAlloc, which returns an error if the allocation fails. |
Beta Was this translation helpful? Give feedback.
-
|
I understand. What about, instead, returning an additional integer, holding the value of the amount of memory allocated by a function when it is called, so that, it would be possible to calculate (optionally) the total amount of memory in use at any time, and in particular make sure that all memory is released before the program exists. C3, Odin and Zig have allocators which check for memory leaks (in debug mode). |
Beta Was this translation helpful? Give feedback.
-
|
That would be a poor man's version of AddressSanitizer, which can check for such leaks and many other problems. I suggest you simply enable it by adding For example, if you have this leaky program: package main
import "solod.dev/so/mem"
type Point struct {
X int
Y int
}
func main() {
p := mem.Alloc[Point](nil)
p.X = 10
p.Y = 20
println(p.X, p.Y)
}And you run it with the address sanitizer enabled: You'll immediately see the problem: |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
So that lack of de-allocation can be inferred automatically at compile time (without impact on runtime performance)... and optionally displayed in the code editor.
Beta Was this translation helpful? Give feedback.
All reactions