assigning pointer to NULL

I

Ian Collins

Roman said:
Hello,

is it legal to destroy the memory pointed to by a pointer and then assign
NULL to it? Say, like this:

char *ptr;
ptr = malloc(10);
free(ptr);
ptr = NULL;
Yes, you can assign what ever you like to a freed pointer. Assignment
to NULL is often used to mark the memory as freed.
 
S

santosh

Roman said:
Hello,

is it legal to destroy the memory pointed to by a pointer and then
assign NULL to it? Say, like this:

char *ptr;
ptr = malloc(10);
free(ptr);
ptr = NULL;

Certainly. This is common practise.
 
R

Richard Bos

Roman Mashak said:
is it legal to destroy the memory pointed to by a pointer and then assign
NULL to it? Say, like this:

char *ptr;
ptr = malloc(10);
free(ptr);
ptr = NULL;

Legal, but useless. If you think you've found the royal road to memory
management, consider the following:

char *ptr, &ptr2;
ptr=malloc(10);
ptr2=ptr+2;
free(ptr);
ptr=NULL;
if (ptr+2) crash_hard_and_without_grace();

Richard
 
M

Malcolm McLean

Roman Mashak said:
is it legal to destroy the memory pointed to by a pointer and then assign
NULL to it? Say, like this:

char *ptr;
ptr = malloc(10);
free(ptr);
ptr = NULL;
Yes. As Richard Bos says. it won't solve all your memory management
problems. But if the pointer will not immediately go out of scope, it is
good idea to make it null to mark that it is now invalid.
 
V

vippstar

Hello,

is it legal to destroy the memory pointed to by a pointer and then assign
NULL to it? Say, like this:

char *ptr;
ptr = malloc(10);
free(ptr);
ptr = NULL;

With best regards, Roman Mashak. E-mail: <removed>

This is done because then you can do free(ptr); which will be
free(NULL); and that does nothing.
 
V

vippstar

Historically, that was not always the case.
True, back then I suppose they assigned a freed pointer to NULL so
when they use a freed pointer they get a NULL address access
violation.
 
D

dj3vande

Legal, but useless.

Not completely useless, only mostly.

If you know that the pointer you're setting to NULL was the only
pointer into that memory, then it's perfectly safe and useful.
(This is rare, but not unheard of.)

It's also not unreasonable to use the non-null-ness of a pointer as a
flag that you have useful information of some sort, and to want to
discard that information before you have something else to replace it
with.
(There are intelligent people who disagree with other intelligent
people about whether this indicates a design flaw.)


dave
 
Y

ymuntyan

Not completely useless, only mostly.

If you know that the pointer you're setting to NULL was the only
pointer into that memory, then it's perfectly safe and useful.
(This is rare, but not unheard of.)

It's also not unreasonable to use the non-null-ness of a pointer as a
flag that you have useful information of some sort, and to want to
discard that information before you have something else to replace it
with.
(There are intelligent people who disagree with other intelligent
people about whether this indicates a design flaw.)

It's UB if you access the value of the pointer after you
call free() on it. So you simply can't (in comp.lang.c)
do stuff like

free(ptr);
....
if (ptr)
it_wasnt_null();

Yevgen
 
K

karthikbalaguru

Hello,

is it legal to destroy the memory pointed to by a pointer and then assign
NULL to it? Say, like this:

char *ptr;
ptr = malloc(10);
free(ptr);

Here, ptr will become a Dangling Pointer.
ptr = NULL;

By this assignment of NULL, ptr will no longer be a dangling pointer.

This is a very important step to be followed while freeing .

Karthik Balaguru
 
C

Chris Dollin

karthikbalaguru said:
Here, ptr will become a Dangling Pointer.


By this assignment of NULL, ptr will no longer be a dangling pointer.

This is a very important step to be followed while freeing .

This is /sometimes/ an important step. At other times, it's
mindblowingly pointless. It depends, for example, on whether
there are other ways to get to the pointer variable (if not,
assigning null to it is pointless) and whether those other
ways bother to check for null (in which case you have problems
regardless).

In short, there is no royal road to memory management in C.
 
F

Flash Gordon

It's UB if you access the value of the pointer after you
call free() on it. So you simply can't (in comp.lang.c)
do stuff like

free(ptr);
...
if (ptr)
it_wasnt_null();

However it is legal to do:

if (something) {
free(ptr);
ptr = NULL;
}
...
if (ptr)
if_was_still_valid();
 
M

Malcolm McLean

Chris Dollin said:
This is /sometimes/ an important step. At other times, it's
mindblowingly pointless. It depends, for example, on whether
there are other ways to get to the pointer variable (if not,
assigning null to it is pointless) and whether those other
ways bother to check for null (in which case you have problems
regardless).

In short, there is no royal road to memory management in C.
No. A good rule is one pointer to each object, but this goes against the
principle of structured programming that variables should be local. Also,
some data structures, like doubly linked lists, depend on more than one
pointer to each node.
However if you allocate and destroy whole strucutres in matching creat /
free functions, your dangling pointer problems should be minimal.
 
A

Army1987

karthikbalaguru said:
By this assignment of NULL, ptr will no longer be a dangling pointer.

This is a very important step to be followed while freeing .
Not necessarily, for example, in many cases the pointer to freed memory is
going to immediately go out of scope.
 
U

user923005

Legal, but useless. If you think you've found the royal road to memory
management, consider the following:

  char *ptr, &ptr2;
  ptr=malloc(10);
  ptr2=ptr+2;
  free(ptr);
  ptr=NULL;
  if (ptr+2) crash_hard_and_without_grace();

I don't think there are any magical cures for undefined behavior
caused by programming errors. The program may also crash hard and
without grace at the free(ptr) function call (or worse, of course).
In general, I think that setting a freed pointer to null is a good
idea. The fact that it won't cure every sort of pointer illness
doesn't negate the small value received in recompense.

IMO-YMMV.

P.S.
I almost always set freed pointers to null myself (the exception being
in C++ because it is pointless to set a deleted class member pointer
to null in a destructor.)
It helps with a very small class of problems (but does have one bad
side effect -- double frees are less likely to be detected) but
overall I think it is a tiny win.
 
R

Roman Mashak

Hello,

is it legal to destroy the memory pointed to by a pointer and then assign
NULL to it? Say, like this:

char *ptr;
ptr = malloc(10);
free(ptr);
ptr = NULL;

With best regards, Roman Mashak. E-mail: (e-mail address removed)
 
J

Joachim Schmitz

Chris said:
This is /sometimes/ an important step. At other times, it's
mindblowingly pointless. It depends, for example, on whether
there are other ways to get to the pointer variable (if not,
assigning null to it is pointless) and whether those other
ways bother to check for null (in which case you have problems
regardless).
Not quite: you program will crash right there, when dereferencing the NULL
pointer, rather than at some other obscure and random place, so this helps
in detecting the bug.

Bye, Jojo
 
J

Joachim Schmitz

Army1987 said:
Not necessarily, for example, in many cases the pointer to freed
memory is going to immediately go out of scope.
In which case there is some work to be done by the optimizer: detect this
and drop the assignment

Bye, Jojo
 
J

Joachim Schmitz

user923005 said:
On Jan 30, 1:01 am, (e-mail address removed) (Richard Bos) wrote:
P.S.
I almost always set freed pointers to null myself (the exception being
in C++ because it is pointless to set a deleted class member pointer
to null in a destructor.)
It helps with a very small class of problems (but does have one bad
side effect -- double frees are less likely to be detected)
But they won't harm (i.e. possibly crash the program) anymore either.

Bye, Jojo
 
I

Ian Collins

Joachim said:
But they won't harm (i.e. possibly crash the program) anymore either.
But the effect can be detrimental on a system (which includes most
desktop environments) where the allocator can detect duplicate frees.

For that reason, I never set a freed pointer to NULL.
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top