Re: free()
- From: raxitsheth2000@xxxxxxxxxxx
- Date: 14 Feb 2007 03:56:40 -0800
On Feb 14, 4:00 pm, Richard Heathfield <r...@xxxxxxxxxxxxxxx> wrote:
raxitsheth2...@xxxxxxxxxxx said:i agree on most of your suggestion
On Feb 14, 2:33 pm, Richard Heathfield <r...@xxxxxxxxxxxxxxx> wrote:
raxitsheth2...@xxxxxxxxxxx said:
<snip>
p=(char *) malloc(100);
The cast is unnecessary, as I have already explained to you.agree, but still its hard to unlearn the stuff learnt from book,
"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.
may you have example that show the things and Don't Break rule for
what may be the behaviour of accessing memory after freeing even if
one SHOULD NOT access it. ?
I 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 yourYes, this is correct if programmer is very very very careful and Dont
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/ ?
what may be wrong if one is knowing the sizeof allocated memory and
memset with 0, and free it (and may be assign NULL to var) ? (except
performance--i agree.)
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.
thanks for this,
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
email: rjh at the above domain, - www.
--Raxit
.
- Follow-Ups:
- Re: free()
- From: santosh
- Re: free()
- From: Richard Heathfield
- Re: free()
- References:
- free()
- From: dbansal
- Re: free()
- From: Richard Heathfield
- Re: free()
- From: raxitsheth2000
- Re: free()
- From: Richard Heathfield
- Re: free()
- From: raxitsheth2000
- Re: free()
- From: Richard Heathfield
- free()
- Prev by Date: Re: Macro substitution: curly braces behaviour?
- Next by Date: Re: free()
- Previous by thread: Re: free()
- Next by thread: Re: free()
- Index(es):
Relevant Pages
|