473,608 Members | 1,809 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Setting pointers to null after freeing them

Hi,

My question is related to setting freed pointers to NULL.

After freeing a pointer:
1) Should the freeing routine also be responsible for setting the
pointer to null?
2) Or, should the client/user code be responsible for doing it?

On what basis should a decision be made favouring either case ?

Rgds.
Amogh
May 2 '06 #1
5 3156
Amogh wrote:
Hi,

My question is related to setting freed pointers to NULL.

After freeing a pointer:
1) Should the freeing routine also be responsible for setting the
pointer to null?
2) Or, should the client/user code be responsible for doing it?

On what basis should a decision be made favouring either case ?


Most of the time, a freed pointer does not get set to null. Typically,
when the object is no longer necessary, it is freed, and then the
pointer itself is deallocated. This may occur with an explicit free
statement, or it may happen when the function returns and its automatic
variables are lost.

The following example code contains two "freeing routines". Neither the
routine itself, nor the client/user code actually sets any pointers to null.

Note that pointers are passed by value in C. If the freeing routine
wanted to set the caller's pointer to NULL, it would have to take a
pointer to that pointer. That seems like poor style to me.

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

typedef struct {
int year;
int month;
int day;
} Date;

Date *new_date(int year, int month, int day)
{
Date *d = malloc(sizeof *d);
if(!d) return NULL;

d->year = year;
d->month = month;
d->day = day;

return d;
}

void destroy_date(Da te *d)
{
free(d);

/* There is no point setting 'd = 0' here, as
the value of d is lost when the function
returns anyway.

One would have to write the function as:
void destroy_date(Da te **pd)
{
free(*pd);
*pd = NULL;
}
and the caller would call it as
destroy_date(&d ate);
that would actually set the caller's 'date' variable to null.
I don't recommend doing this.
*/
}

typedef struct {
char *name;
Date *birthdate;
} Person;

Person *new_person(con st char *name, int year, int month, int day)
{
Person *p = malloc(sizeof *p);
if(!p) return NULL;

p->name = malloc(strlen(n ame) + 1);
if(!p->name)
{
free(p);
return NULL;
}
strcpy(p->name, name);

p->birthdate = new_date(year, month, day);
if(!p->birthdate)
{
free(p->name);
free(p);
return NULL;
}

return p;
}

void destroy_person( Person *p)
{
free(p->name);
destroy_date(p->birthdate);
free(p);
}

const char *ordinal(int n)
{
if(n % 100 == 11 || n % 100 == 12 || n % 100 == 13) return "th";
if(n % 10 == 1) return "st";
if(n % 10 == 2) return "nd";
if(n % 10 == 3) return "rd";
return "th";
}

const char *monthname(int n)
{
const char *names[] = {
"January", "February", "March", "April",
"May", "June", "July", "August",
"September" , "October", "November", "December"} ;

if(n >= 1 && n <= 12)
return names[n - 1];
else
return NULL;
}

void show_person(con st Person *p)
{
printf("%s was born on the %d%s of %s, %d\n",
p->name,
p->birthdate->day,
ordinal(p->birthdate->day),
monthname(p->birthdate->month),
p->birthdate->year);
}

int main(void)
{
size_t i;
Person *family[5];
family[0] = new_person("Sim on", 1982, 9, 6);
family[1] = new_person("And rew", 1986, 2, 25);
family[2] = new_person("Tho mas", 1987, 6, 5);
family[3] = new_person("Sue ", 1957, 5, 27);
family[4] = new_person("Mic hael", 1953, 2, 6);

for(i = 0; i < sizeof family / sizeof *family; i++)
{
if(family[i])
{
show_person(fam ily[i]);
destroy_person( family[i]);
}
}
return 0;
}

--
Simon.
May 2 '06 #2
Amogh opined:
Hi,

My question is related to setting freed pointers to NULL.

After freeing a pointer:
1) Should the freeing routine also be responsible for setting the
pointer to null?
2) Or, should the client/user code be responsible for doing it?

On what basis should a decision be made favouring either case ?


Standard C function `free()` that de-allocates memory pointed to by its
argument does not modify the pointer in question.

Whether you need this pointer reset to NULL after memory's been freed
is really a design choice for your particular application. I guess the
most important thing is, once you make a choice of who resets the
pointers (and whether they're reset at all) you stick to it throughout
the application.

My personal preference would likely be for the freeing routine to reset
the pointer to NULL, but as I said it really depends on the job at
hand. I'm sure arguments can be found for both.

--
The light at the end of the tunnel is the headlight of an approaching
train.

<http://clc-wiki.net/wiki/Introduction_to _comp.lang.c>

May 2 '06 #3
On 2006-05-02, Amogh <am*****@nospam .com> wrote:
Hi,

My question is related to setting freed pointers to NULL.

After freeing a pointer:
1) Should the freeing routine also be responsible for setting the
pointer to null?
2) Or, should the client/user code be responsible for doing it?

On what basis should a decision be made favouring either case ?


It's a matter of taste really. I prefer (2) to (1).

The problem with (1) is you have to use pointers-to-pointers, and
there's no way the called routine can know about aliases anyway.
Example:

#include <stdlib.h>

void destroy(int **p)
{
free(*p);
*p = NULL;
}

int main(void)
{
int *p = malloc(100);
int *q = p;

free(&p);
/* OK p is NULL, but I can still use q... */

return 0;
}

The other benefit of (2) is that it mirrors the way free works, so is
clearer to most people. Otherwise the reader of the code has to ask,
this is pointer-to-pointer, why?

Some people use macros:

#define DESTROY(p) do { FREE(p); p = NULL; } while 0

or something, to simulate "pass-by-reference"; I'd really advise
against this though.
May 2 '06 #4
Simon Biber wrote:
Amogh wrote: -snip-

int main(void)
{
size_t i;
Person *family[5];
family[0] = new_person("Sim on", 1982, 9, 6);
family[1] = new_person("And rew", 1986, 2, 25);
family[2] = new_person("Tho mas", 1987, 6, 5);
family[3] = new_person("Sue ", 1957, 5, 27);
family[4] = new_person("Mic hael", 1953, 2, 6);

for(i = 0; i < sizeof family / sizeof *family; i++)
{
if(family[i])
{
show_person(fam ily[i]);
destroy_person( family[i]);
}
}
return 0;
}


I know it isn't, but it looks very much like C++.... The functions are
like from inside C++ classes... Did you take it from some C++ code? :-)

Again: I know it's C code but perhaps a bit C++'ish?... Nothing wrong
with that however... Actually I like the example...
Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
May 2 '06 #5
Martin Jørgensen wrote:
Simon Biber wrote:

int main(void)
{
size_t i;
Person *family[5];
family[0] = new_person("Sim on", 1982, 9, 6);
family[1] = new_person("And rew", 1986, 2, 25);
family[2] = new_person("Tho mas", 1987, 6, 5);
family[3] = new_person("Sue ", 1957, 5, 27);
family[4] = new_person("Mic hael", 1953, 2, 6);

for(i = 0; i < sizeof family / sizeof *family; i++)
{
if(family[i])
{
show_person(fam ily[i]);
destroy_person( family[i]);
}
}
return 0;
}

I know it isn't, but it looks very much like C++.... The functions are
like from inside C++ classes... Did you take it from some C++ code? :-)


No, I wrote it from scratch. I have been doing more object-oriented
programming lately so I suppose it has rubbed off on my C code too.
Again: I know it's C code but perhaps a bit C++'ish?... Nothing wrong
with that however... Actually I like the example...


Thanks.

--
Simon.
May 2 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2648
by: mosfets | last post by:
Hi, I'm having a little trouble figuring out the difference in terms of memory allocation between: class person_info; class A { private:
8
448
by: John Hanley | last post by:
I working in C. I haven't paid much attention to void pointers in the past, but I am wondering if I can use them for my various linked lists to save work. I have two different linked lists that each use nodes of different structs. However the principle of my adding to the linked lists and removing them is the same. So will it work to have these functions accept a void pointer as an argument for the list and a void pointer as an...
7
2204
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ http://www.eskimo.com/~scs/C-faq/faq.html but it doesn't seem to answer my questions... So, I've made an example behind, with some included questions...
7
1754
by: Akhil | last post by:
How much is Pointers different in C# compared to C++? Do we have destructors in C#? if yes, for what do we use it? -- Cheers, Akhil
31
2326
by: Yevgen Muntyan | last post by:
6.2.4 of standard says: "The value of a pointer becomes indeterminate when the object it points to reaches the end of its lifetime." Do I understand it right that value of pointer may or may not change? What I am wondering about is this: int a_func (void) { char *s = malloc (10);
20
1506
by: sam_cit | last post by:
Hi Everyone, I just heard from a friend of mine that there are few c compilers that give an error when pointers are not initialised to NULL. Is it correct? and if so, is there any standard for that? Thanks in advance...
11
4231
by: Piotrek | last post by:
Hi, I have no idea why my reallocation function (myadd) is breaking up my array. I'm trying to solve it and it took me few hours already. Maybe you can look at my code and explain me what is wrong. (I still have problems with pointers). I left my code as it is. Thank you! #include "stdafx.h" /* contains stdio.h */ #include <stdlib.h>
27
2170
by: rocco.rossi | last post by:
I've been trying to write a function capable of checking if a pointer value is set to NULL, and if it isn't, of deallocating and setting it's value to NULL regardless of the pointer's type. I thought "void ** " should do the trick, but I kept getting the following warning message from gcc: warning: passing argument 1 of 'free_var' from incompatible pointer type which obviously could be solved by doing an explicit cast to (void **)
3
13791
by: JoxC | last post by:
Hey guys, I was wondering if there is an *efficient* way in setting every pointer in an array of pointers to NULL? memset cannot work for compatibility reasons, but I was playing a bit with memcpy: I built an array of NULL-pointers and tried to memcopy it to the array I wish to clear, but I couldn't find a working solution. e.g.: int** NULL_ARRAY; NULL_ARRAY = (int **) malloc (x * sizeof(int*)); int** array; array = (int **) malloc (x *...
0
8069
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8011
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8503
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8488
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8160
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6826
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
3972
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4036
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1611
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.