freeing pointers that are created in a function

G

G G

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?
 
J

James Kuyper

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.
 
I

Ian Collins

G said:
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.
 
K

Kaz Kylheku

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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top