Re : Re : memory allocation and freeing memory






Le 13/06/2005 23:22, dans d8ktec$kou$1@xxxxxxxxxxxxxxxxxxxxxxxx, « Eric
Sosman » <eric.sosman@xxxxxxx> a écrit :

>
>
> Jean-Claude Arbaut wrote:
>>> I personally tend to add a '_dyn' suffix to functions returning
>>> something to be freed to make it self-documenting...
>>
>>
>> A good practice (I think, I'm not a guru): a function never
>> returns a block it has allocated. This means that the caller
>> never frees memory, and the callee always frees all its
>> allocated blocks before function returns. If the callee needs
>> to return a block, then the caller allocates it and pass it
>> as an argument. Maybe there are situations where it doesn't work ?
>
> An approach I've found useful is to write pairs of
> functions: one function allocates and initializes memory
> for a Whatever and returns a Whatever*, while the other
> accepts a Whatever* argument and does whatever is needed
> to "de-initialize" the Whatever, including releasing its
> memory. The advantage is that the caller never needs to
> the details of how a Whatever is built: how much memory
> is needed, whether it's all in one chunk or is built from
> several pieces linked together, and so on.
>
> For example, consider fopen() and fclose(). As the
> caller you do not need to know anything about what a `FILE'
> looks like; all you care about is the `FILE*'. You do not
> know whether the `FILE' is allocated dynamically or statically,
> what extra buffers and such may be allocated along with it,
> and so on -- all you need to know is that fopen() produces a
> `FILE*', and that fopen() cleans it up when you're done.
>
> The same pattern works well for "purely memory" constructs,
> too. I've written a little expression evaluator that has
> three (principal) functions in its interface: a compiler that
> transforms the source expression into an "opaque" data type
> that's allocated during compilation, an evaluator that takes
> the opaque pointer and an array of user-supplied variable
> values and returns the expression's value, and a destructor
> that accepts the opaque pointer and discards the memory it
> uses. The current version of the destructor is fairly simple:
>
> void ExprDestroy(Expr *expr) {
> free (expr);
> }
>
> ... but by packaging it where the caller can't see the details
>
> I retain the freedom to change my mind about the way memory is
> managed, and perhaps do something like
>
> void ExprDestroy(Expr *expr) {
> free (expr->constants);
> free (expr->bytecodes);
> #ifndef NDEBUG
> free (expr->debugging_info);
> #endif
> free (expr);
> }
>
> The caller never needs to know how things are done "behind the
> curtain."

Ok, I was late in my answer ;-)

Indeed, I have already used this approach, I wonder why I didn't
Remember that earlier... Shame on me !

.



Relevant Pages

  • Re: Re : memory allocation and freeing memory
    ... This means that the caller ... > never frees memory, and the callee always frees all its ... then the caller allocates it and pass it ... that accepts the opaque pointer and discards the memory it ...
    (comp.lang.c)
  • Re: Consuming a DLL in Visual Basic
    ... > string by using a method such as LocalFree. ... This is all about who allocates the memory and who cleans it up for strings ... newly allocated object back to a caller without creating a memory leak. ...
    (microsoft.public.dotnet.framework)
  • Re: acl_from_text leaking memory
    ... The leak occurs because of a temporary string ... that acl_from_text allocates to parse the text. ... "...This function may cause memory to be allocated. ... manpage still should not be changed as the caller is still responsible ...
    (freebsd-hackers)
  • Re: Framework 2.0 array redim unsatisfactory performance
    ... implementation details of Redim, Redim Preserve, and List. ... significantly higher object allocated overhead then List. ... ReDim simply allocates a new ... but I do not even attempt to test its memory ...
    (microsoft.public.dotnet.languages.vb)
  • Re: CE6.0 Driver Pointer Marshalling - passing pointers out only?
    ... This is an array of pointers to buffers that get set up ... the driver allocates the buffers (via an internal allocation ... routine from its own block of reserved memory). ... So I can map ...
    (microsoft.public.windowsce.platbuilder)

Loading