Home

On Aug 10, 8:50 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
> PicO wrote:
> > so i can't return reference :( ..
>
> A reference to what? You need an object that survives the function
> in order to use the reference to it, don't you? You _can_ return
> a reference, it just has to be to the object that is still alive
> when you use that reference.

Your options are limited: either the calling function needs to provide
a buffer, accept ownership of some returned data, or the called
function must own the data. In the latter case, this could be through
static data or class member data, but the latter is problematic if the
class containing the called function is "destruct"ed before the caller
has finished using the data. In practice, callee-owned data is often
useful for performance reasons, but it's often hackish to implement.
Singleton patterns might help, but it's still problematic to guarantee
a singleton instance doesn't go out of scope before the caller
finishes with the data: see Modern C++ Design for a discussion and
implementation alternatives. Having the called function own the data
is also problematic in that it often obliges the caller to make
further calls to explicitly free the memory, and of course expecting
your client code to do anything reliably is a recipe for trouble.
Thankfully, a memory leak isn't _usually_ critical.

previous
next

Re: Variable Memory Usage
Re: List Question
handling modules in packages
Re: beginner, idiomatic python
Re: C/C++ guidelines
Akogo
Fundacja Avalon
Fundacja Hobbit
Krwinka
Mimo Wszystko