Setting pointers to null after freeing them

A

Amogh

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
 
S

Simon Biber

Amogh said:
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(Date *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(Date **pd)
{
free(*pd);
*pd = NULL;
}
and the caller would call it as
destroy_date(&date);
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(const char *name, int year, int month, int day)
{
Person *p = malloc(sizeof *p);
if(!p) return NULL;

p->name = malloc(strlen(name) + 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(const 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("Simon", 1982, 9, 6);
family[1] = new_person("Andrew", 1986, 2, 25);
family[2] = new_person("Thomas", 1987, 6, 5);
family[3] = new_person("Sue", 1957, 5, 27);
family[4] = new_person("Michael", 1953, 2, 6);

for(i = 0; i < sizeof family / sizeof *family; i++)
{
if(family)
{
show_person(family);
destroy_person(family);
}
}
return 0;
}
 
V

Vladimir Oka

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

Ben C

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

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

Simon said:
Amogh wrote: -snip-


int main(void)
{
size_t i;
Person *family[5];
family[0] = new_person("Simon", 1982, 9, 6);
family[1] = new_person("Andrew", 1986, 2, 25);
family[2] = new_person("Thomas", 1987, 6, 5);
family[3] = new_person("Sue", 1957, 5, 27);
family[4] = new_person("Michael", 1953, 2, 6);

for(i = 0; i < sizeof family / sizeof *family; i++)
{
if(family)
{
show_person(family);
destroy_person(family);
}
}
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
 
S

Simon Biber

Martin said:
Simon said:
int main(void)
{
size_t i;
Person *family[5];
family[0] = new_person("Simon", 1982, 9, 6);
family[1] = new_person("Andrew", 1986, 2, 25);
family[2] = new_person("Thomas", 1987, 6, 5);
family[3] = new_person("Sue", 1957, 5, 27);
family[4] = new_person("Michael", 1953, 2, 6);

for(i = 0; i < sizeof family / sizeof *family; i++)
{
if(family)
{
show_person(family);
destroy_person(family);
}
}
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.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top