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


Groups > comp.lang.c > #43383

Re: freeing pointers that are created in a function

From Kaz Kylheku <kaz@kylheku.com>
Newsgroups comp.lang.c
Subject Re: freeing pointers that are created in a function
Date 2014-04-22 22:28 +0000
Organization Aioe.org NNTP Server
Message-ID <20140422152345.562@kylheku.com> (permalink)
References <33316bf9-6341-4a32-88a6-b2d4360e4f57@googlegroups.com>

Show all headers | View raw


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.

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web