Re: free()



dbansal wrote:
Hi group,

I have a question to ask you all. I have allocated some chunk of
memory using ptr=(int*)malloc().

The cast is not needed, indeed even counterproductive, in C.

now I am trying to free that memory
using free((void*)ptr).

Same as above.

My question is does free() make ptr NULL?

No.

If not how do we know whether ptr memory has been freed already and we
should not try to free the same memory twice. Man pages does not tell
anything about this behavoiur.

As long as you pass a value previously obtained from malloc() calloc()
or realloc(), free() will succeed. If the argument is a null pointer
value, free() does nothing.

There's no mechanism in C to tell you if a pointer value points to
validly addressable memory or not. You'll have to keep track of such
things yourself. One safe practise is to immediately set a pointer to
NULL after calling free() on it. Passing a null pointer to free() is
harmless.

.



Relevant Pages

  • 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)
  • Re: Is this math test too easy?
    ... > communications glitch; one of the more laughable cartoons ... it was loaded into physical memory and, ... > Or one can interpret the character string as one of the values ... A pointer to an integer? ...
    (sci.math)
  • Re: grow list by tail, pointer example recipe -- please comment
    ... manufacturing a pointer with that address. ... the next cons cell. ... believe these lists are in consecutive memory locations. ...
    (comp.lang.lisp)
  • Re: some unanswered questions on C
    ... A pointer variable that's never been given a value. ... you don't know what memory you're modifying. ... >what i want to ask is that when i declare my buffer for fgets as ... "char *buffer" creates a pointer, ...
    (comp.unix.programmer)
  • Re: "Mastering C Pointers"....
    ... all means go ahead and dive right into the C language. ... Memory is a separate unit which just stores bits. ... A pointer at the hardware level _is an integer_. ... since loops make your logic more much ...
    (comp.lang.c)