Re: free()
- From: Richard Heathfield <rjh@xxxxxxxxxxxxxxx>
- Date: Wed, 14 Feb 2007 11:00:37 +0000
raxitsheth2000@xxxxxxxxxxx said:
On Feb 14, 2:33 pm, Richard Heathfield <r...@xxxxxxxxxxxxxxx> wrote:<snip>
raxitsheth2...@xxxxxxxxxxx said:
agree, but still its hard to unlearn the stuff learnt from book,
p=(char *) malloc(100);
The cast is unnecessary, as I have already explained to you.
"malloc returns void * and you need to cast it"
It's because malloc returns void * that you _don't_ need to cast it!
<snip>
printf("\n AFTER FREE ");
printf("\nP=%p : %s",p,p);
That's illegal, because you are using the value of an indeterminate
pointer.
Yes, I Agree, its illegal.
But here we are trying to find out some stuff on what happen/can be
done to avoid illegal access after freeing the memory,
Writing programs whose output is undefined is not a good way to find
stuff out.
I may want to learn if we use
free(p);p=NULL;
kind of stuff, is it anyhow helpful in avoiding this type of Illegal
stuff ?
I find it helpful to ensure that any object pointer *either* points to a
valid object *or* has a value of NULL. If I do this, then I can be
assured that if(p != NULL) { p points to a valid object } but the price
of this assurance is that I must set p to NULL if it no longer points
to a valid object.
<snip>
output on linux box
...is irrelevant, because you broke the rules of C, so any output you
get is not bound by those rules.
we are discussing how to avoid of breaking C Rules ( or better what
may be outcome of accessing freed memory) and to explain this i need
to break the rules.(Correct me if i done wrong. or better example.)
You're wrong. Breaking the rules teaches you nothing about the language,
because by breaking the rules you are giving the implementation the
freedom to provide any behaviour whatsoever, or even no behaviour
whatsoever if it prefers.
Yes, this is correct if programmer is very very very careful and DontI think (correct me if I am wrong, I am learning here.)
1.this technique is useful in some case but not in all where
pointer assignment is doing like this way. q is pointing to same
memory location as of p before free, and doing p=NULL is not
helpful for q.
If you give q a value which later becomes indeterminate, it's your
job to make sure you don't evaluate q.
work under difficult deadlines.
Yes, programmers do have to be very very very careful. Because deadlines
are often unrealistic, mistakes happen. With experience, you will learn
how to identify these mistakes quickly.
any opinion on this ? Adv/Disadv/limitation/ ?
I recommend that you study the rules of C carefully before you decide
on a programming strategy for memory management.
can you recommend good resource ?
ISO/IEC 9899.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
.
- Follow-Ups:
- Re: free()
- From: raxitsheth2000
- Re: free()
- References:
- free()
- From: dbansal
- Re: free()
- From: Richard Heathfield
- Re: free()
- From: raxitsheth2000
- Re: free()
- From: Richard Heathfield
- Re: free()
- From: raxitsheth2000
- free()
- Prev by Date: Re: free()
- Next by Date: Re: free()
- Previous by thread: Re: free()
- Next by thread: Re: free()
- Index(es):
Relevant Pages
|