freeing pointers that are created in a function

Discussion in 'C Programming' started by G G, Apr 22, 2014.

  1. G G

    G G Guest

    let's say, in a function, malloc or calloc is called to allocate memory. the function returns a pointer to that allocated space.

    question: once the function ends the only pointer to the allocated memory is that being assigned by the calling function.

    question: freeing the pointer being assigned to from the returning function is the only one that needs to be freed in order to keep from having a memory leak?
     
    G G, Apr 22, 2014
    #1
    1. Advertisements

  2. G G

    James Kuyper Guest

    The only values that can safely be passed to free() are values that were
    returned by previous calls to malloc(), calloc(), or realloc(); those
    values, if not null, point to the start of the block of memory that was
    allocated.

    One such pointer value needs to be free()d. If you don't have such a
    pointer value saved somewhere, and cannot recreate such a pointer (for
    instance, by subtracting from a pointer to some other part of the
    allocated memory), you have a memory leak.

    Any use of any pointer into the block of allocated memory has undefined
    behavior after the call to free(), whether or not it's the one that you
    passed to free(). In particular, it's undefined behavior to pass such
    values to any function; free() itself is no exception to that rule.
     
    James Kuyper, Apr 22, 2014
    #2
    1. Advertisements

  3. G G

    Ian Collins Guest

    [Please wrap your lines and remove the rubbish the awful google
    interface adds!]

    In short, yes. One allocation, one free.
     
    Ian Collins, Apr 22, 2014
    #3
  4. G G

    Kaz Kylheku Guest

    All copies of such a pointer are the same value, referencing the same object.

    When free is called on any copy of the value, it destroys the object, and all
    other copies of the pointer become indeterminate.

    It is not possible to call free without making a copy of the pointer, because
    C function arguments have pass-by-value semantics. When you call free(p),
    the expression p is evaluated to produce a value, and a copy of that value
    travels into the free function, not the expression p itself.

    This is the same as if you cancel a contract. All photocopies of the contract
    documents become invalid, because they refer to the same contract.

    Or, when you sell your car, you do not have to cancel both the front and rear
    license plate; they are the same thing.
     
    Kaz Kylheku, Apr 22, 2014
    #4
  5. G G

    G G Guest

    On Tuesday, April 22, 2014 5:30:59 PM UTC-4, G G wrote:

    thanks everyone,

    g.
     
    G G, Apr 23, 2014
    #5
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.