Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Kaz Kylheku <kaz@kylheku.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: free a pointer and have it point to NULL |
| Date | 2014-04-22 22:41 +0000 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <20140422153030.173@kylheku.com> (permalink) |
| References | <639a5283-8e4b-4f9a-aa7f-ef2c88174768@googlegroups.com> <bro63iFojnqU2@mid.uni-berlin.de> <1b61m1uhvh.fsf@snowball.wb.pfeifferfamily.net> |
On 2014-04-22, Joe Pfeiffer <pfeiffer@cs.nmsu.edu> wrote:
> jt@toerring.de (Jens Thoms Toerring) writes:
>
>> G G <gdotone@gmail.com> wrote:
>>> memory allocated on the heap once freed should the pointer be assigned NULL?
>>
>> It can't hurt since any attempt to use the now invalid pointer
>> again would lead to an immediate crash of the program. But then
>> it depends a bit on circumstances: if the free() is at the end
>> of a function and there's no chance that the pointer is acci-
>> dentally used again, then there's not much good it will do.
>> And, of course, each free() will have to be followed by
>> another line of code, which can become tedious and make
>> things a bit harder to read - unless you use a macro like
>>
>> #define safe_free( addr ) \
>> do { \
>> free( addr ); \
>> addr = NULL; \
>> } while ( 1 == 0 )
>>
>> instead of free() everywhere. I've done that in several cases
>> where I felt the need to be paranoid;-)
>
> Unfortunately, this doesn't help with situations like
>
> a = malloc(size);
> b = a;
> safe_free(a);
>
> so it's always struck me as a giving a false sense of security.
it helps in situations like
safe_free (proxy->obj); /* proxy is the only gateway to obj */
In situations where you you deal with the shared reference problem, perhaps
with reference counting, it also helps. Even if proxy is not the only way to
reach obj, it makes sense to do:
drop_ref (proxy->obj); /* drop_ref is a macro which also sets obj to null */
proxy may well have had the last reference to obj. We have no right to access
obj through proxy, even if obj still exists. Since we have no right to access
obj through proxy, it's best to null out the pointer, so that any attempt to do
so is flagged as a null pointer dereference.
In other words, the management of multiple copies is a separate, problem; it
doesn't invalidate the setting of just one copy to null, which can still a
valuable approach when we have somehow addressed the management of sharing.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
free a pointer and have it point to NULL G G <gdotone@gmail.com> - 2014-04-22 14:22 -0700
Re: free a pointer and have it point to NULL James Kuyper <jameskuyper@verizon.net> - 2014-04-22 17:32 -0400
Re: free a pointer and have it point to NULL G G <gdotone@gmail.com> - 2014-04-22 14:36 -0700
Re: free a pointer and have it point to NULL Keith Thompson <kst-u@mib.org> - 2014-04-22 14:37 -0700
Re: free a pointer and have it point to NULL Johannes Bauer <dfnsonfsduifb@gmx.de> - 2014-04-29 13:10 +0200
Re: free a pointer and have it point to NULL Richard <rgrdev_@gmail.com> - 2014-04-29 13:14 +0200
Re: free a pointer and have it point to NULL Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-04-29 13:19 +0100
Re: free a pointer and have it point to NULL Kaz Kylheku <kaz@kylheku.com> - 2014-04-29 13:57 +0000
Re: free a pointer and have it point to NULL Kaz Kylheku <kaz@kylheku.com> - 2014-04-29 13:43 +0000
Re: free a pointer and have it point to NULL James Kuyper <jameskuyper@verizon.net> - 2014-04-29 09:59 -0400
Re: free a pointer and have it point to NULL Keith Thompson <kst-u@mib.org> - 2014-04-29 07:58 -0700
Re: free a pointer and have it point to NULL Keith Thompson <kst-u@mib.org> - 2014-04-29 11:50 -0700
Re: free a pointer and have it point to NULL James Kuyper <jameskuyper@verizon.net> - 2014-04-29 15:47 -0400
Re: free a pointer and have it point to NULL James Kuyper <jameskuyper@verizon.net> - 2014-04-29 12:41 -0400
Re: free a pointer and have it point to NULL Robert Wessel <robertwessel2@yahoo.com> - 2014-04-29 13:15 -0500
Re: free a pointer and have it point to NULL glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-04-29 21:29 +0000
Re: free a pointer and have it point to NULL glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-04-29 15:26 +0000
Re: free a pointer and have it point to NULL jt@toerring.de (Jens Thoms Toerring) - 2014-04-22 21:43 +0000
Re: free a pointer and have it point to NULL Keith Thompson <kst-u@mib.org> - 2014-04-22 14:53 -0700
Re: free a pointer and have it point to NULL Öö Tiib <ootiib@hot.ee> - 2014-04-24 00:38 -0700
Re: free a pointer and have it point to NULL James Kuyper <jameskuyper@verizon.net> - 2014-04-24 07:52 -0400
Re: free a pointer and have it point to NULL <william@wilbur.25thandClement.com> - 2014-04-24 16:13 -0700
Re: free a pointer and have it point to NULL Ian Collins <ian-news@hotmail.com> - 2014-04-25 12:02 +1200
Re: free a pointer and have it point to NULL <william@wilbur.25thandClement.com> - 2014-04-24 22:03 -0700
Re: free a pointer and have it point to NULL Ian Collins <ian-news@hotmail.com> - 2014-04-25 17:22 +1200
Re: free a pointer and have it point to NULL <william@wilbur.25thandClement.com> - 2014-04-24 23:00 -0700
Re: free a pointer and have it point to NULL Öö Tiib <ootiib@hot.ee> - 2014-04-27 08:32 -0700
Re: free a pointer and have it point to NULL Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2014-04-22 16:19 -0600
Re: free a pointer and have it point to NULL Kaz Kylheku <kaz@kylheku.com> - 2014-04-22 22:41 +0000
Re: free a pointer and have it point to NULL Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2014-04-22 18:17 -0600
Re: free a pointer and have it point to NULL Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2014-04-24 08:48 -0600
non-NULL pointers which have the value 0 (Was: Re: free a pointer and have it point to NULL) Andrew Cooper <root@127.0.0.1> - 2014-04-23 03:28 +0100
Re: non-NULL pointers which have the value 0 (Was: Re: free a pointer and have it point to NULL) Kaz Kylheku <kaz@kylheku.com> - 2014-04-23 04:37 +0000
Re: non-NULL pointers which have the value 0 glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-04-23 06:14 +0000
Re: free a pointer and have it point to NULL Kaz Kylheku <kaz@kylheku.com> - 2014-04-22 22:18 +0000
Re: free a pointer and have it point to NULL Michael Angelo Ravera <maravera@prodigy.net> - 2014-05-03 01:00 -0700
csiph-web