infratec wrote:... where the caller is not responsible for freeing the memory:
Why?
I felt "where" when appropriate but I've changed it to "when" to be clearer. The expectation being that the memory will be freed at some point but is NOT by the caller; merely accessed.
infratec wrote:So if you call your procedure in a loop, you get out of memory (if you do it often enough).
Imagine that returned pointer is always the same. It's not allocating any more memory each time it's returned because it always points to the same place.
Giving this a lot of thought, I think that out of all the possible enhancements to PureBasic, extending support of return types to pointers (and interfaces by extension) would give us a lot of added flexibility. I'm already doing things like this to help improve my code's readability:
Code: Select all
Macro POINTER
i
EndMacro
Macro MAP_NODE_POINTER
POINTER
EndMacro
Structure Map_Node
*Left.Map_Node
*Right.Map_Node
...
EndStructure
Declare.MAP_NODE_POINTER Map_CreateNode(_IN Key.MAP_KEY, _IN Value.MAP_VALUE)
Of course I can't actually access the Map_Node result without first assigning it to something. This is merely a standard I use for self-documenting my code.
For structures we could have this:
Code: Select all
Structure Map_Node
*Left.Map_Node
EndStructure
Declare.Map_Node* Map_CreateNode(_IN Key.MAP_KEY, _IN Value.MAP_VALUE)
And for interfaces we could have this:
Code: Select all
Interface Node
getLeft.Node()
EndInterface
Declare.Node CreateNode(_IN Key.MAP_KEY, _IN Value.MAP_VALUE)