Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #43363 > unrolled thread

freeing pointers that are created in a function

Started byG G <gdotone@gmail.com>
First post2014-04-22 14:30 -0700
Last post2014-04-22 22:35 -0700
Articles 5 — 4 participants

Back to article view | Back to comp.lang.c


Contents

  freeing pointers that are created in a function G G <gdotone@gmail.com> - 2014-04-22 14:30 -0700
    Re: freeing pointers that are created in a function James Kuyper <jameskuyper@verizon.net> - 2014-04-22 17:42 -0400
    Re: freeing pointers that are created in a function Ian Collins <ian-news@hotmail.com> - 2014-04-23 09:44 +1200
    Re: freeing pointers that are created in a function Kaz Kylheku <kaz@kylheku.com> - 2014-04-22 22:28 +0000
    Re: freeing pointers that are created in a function G G <gdotone@gmail.com> - 2014-04-22 22:35 -0700

#43363 — freeing pointers that are created in a function

FromG G <gdotone@gmail.com>
Date2014-04-22 14:30 -0700
Subjectfreeing pointers that are created in a function
Message-ID<33316bf9-6341-4a32-88a6-b2d4360e4f57@googlegroups.com>
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?

[toc] | [next] | [standalone]


#43370

FromJames Kuyper <jameskuyper@verizon.net>
Date2014-04-22 17:42 -0400
Message-ID<5356E25C.6010805@verizon.net>
In reply to#43363
On 04/22/2014 05:30 PM, G G wrote:
> 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?

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.

[toc] | [prev] | [next] | [standalone]


#43372

FromIan Collins <ian-news@hotmail.com>
Date2014-04-23 09:44 +1200
Message-ID<bro65kF43v1U2@mid.individual.net>
In reply to#43363
G G wrote:
> 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?

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

In short, yes.  One allocation, one free.

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#43383

FromKaz Kylheku <kaz@kylheku.com>
Date2014-04-22 22:28 +0000
Message-ID<20140422152345.562@kylheku.com>
In reply to#43363
On 2014-04-22, G G <gdotone@gmail.com> wrote:
> 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?

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.

[toc] | [prev] | [next] | [standalone]


#43404

FromG G <gdotone@gmail.com>
Date2014-04-22 22:35 -0700
Message-ID<b07ce0e1-7925-4331-9870-5ba3799aba1b@googlegroups.com>
In reply to#43363
On Tuesday, April 22, 2014 5:30:59 PM UTC-4, G G wrote:

thanks everyone,

g.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web