Re: free()



On Feb 14, 4:00 pm, Richard Heathfield <r...@xxxxxxxxxxxxxxx> wrote:
raxitsheth2...@xxxxxxxxxxx said:



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.
i agree on most of your suggestion

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 your
job to make sure you don't evaluate q.
Yes, this is correct if programmer is very very very careful and Dont
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

.



Relevant Pages

  • Re: [Lit.] Buffer overruns
    ... >>of physical memory. ... But that doesn't seem to be what the other posters mean by "overcommit" ... their requests don't ...
    (sci.crypt)
  • Re: Exploiting the Perl
    ... CO> snip ... This is what shared memory is for. ... asked about shared ram that stays resident outside of processes. ... CO> resort to slow disk if you need fast memory. ...
    (perl.beginners)
  • Re: Snow Leopard: A 64-bit Observation
    ... You can use manual memory management, use less memory, and just cope. ... short of a commitment or promise to ship 64-bit Photoshop CS5. ... Which Windows does not do nearly as well as OS X. ...
    (comp.sys.mac.advocacy)
  • Re: Great SWT Program
    ... the keyboard, even when intermittently using the numpad, arrow keys, ... memory for where the keys are, and having the keyboard in my bottom ...
    (comp.lang.java.programmer)
  • Re: Generic Memory Pointers/Access
    ... > with 3rd party libraries, there often arises the situation in which a ... > is cast at each iteration, ... > initialise the pointer once also. ... > further preferable to cast the memory to any arbitrary type, ...
    (comp.lang.cpp)