Re: realloc() implicit free() ?



CBFalconer wrote:
> tigervamp wrote:
> > Walter Roberson wrote:
> >
> >> If realloc() finds it necessary to move the memory block, then
> >> does it free() the previously allocated block?
> >
> > Yes.
> >
> >> The C89 standard has some reference to undefined behaviour if
> >> one realloc()'s memory that was freed by realloc(), but the only
> >> way explicitly mentioned in the C89 standard to free memory via
> >> realloc() is to realloc() it down to 0 bytes.
> >
> > Passing a pointer to realloc that points to memory free'd by _any_
> > function results in undefined behavior. If you successfully
> > realloc memory which results in the original object being
> > deallocated, you must provide a pointer to the new object created
> > by realloc in subsequent realloc calls as the old object has been
> > freed.
> >
> >> I had always assumed it would automatically free the previous
> >> memory, but is the behaviour instead undefined [or defined as
> >> not happening] ?
> >
> > No, the behavior is well defined by the Standard, see section
> > 7.20.3 of C99, I don't have a C89 copy handy.
>
> No, I don't consider it well defined. It leaves too much up to the
> implementation in the case of zero block size requests. This means
> that NULL is not always a sign of failure, and that a non-NULL
> return is not necessarily freeable or reallocable.
>
> In my nmalloc implementation for DJGPP I have chosen to treat a
> zero block size as a request for one byte, and let the alignment
> mechanisms raise it as they will. This ensures that all successful
> requests return a non-NULL pointer that can safely be used in
> subsequent frees or reallocs. (It also has the convenient to me
> side effect of ensuring space for some record-keeping in my code)
>
> I suspect the standard is written to not invalidate the sloppier
> malloc implementations already out there. In cases like this I
> think it should include a recommended practice.
>

As I was going through the Recent replies on the realloc(),
I got some question and my annalysis on that, so regarding on these
please guide me where I fail on the theoritical and practical
Knowledge. I am not able to read all the thread in the replies as
due to some problem in the web server.

Point 1.

If we do the realloc then it means that we have allocated the
extended memory for the current memory, for which we have
reallocated it. Means I need not to free the previous memory
which I extendend to realloc if compiler allocates memory
(extended memory) from the place where intial memory was allocated.

And we need to free if the memory is allocated by the
(realloc)in the new region.

so the key is to always free the memory when you reallocate
the memory by realloc fucntion.

How much I am correct on the Point 1 ?

Point 2.

what is the diffrence between the calloc() and malloc()
As far As I know the basic diffrence is that
1. malloc takes 1 argumnets while calloc takes two
2. malloc initialise the memory with the garbage values while
calloc initialise it with 0 (Zero)
3. malloc allocates continious memeory i.e one Block while
calloc alloactes into the Block
calloc (100, 2) ,means two block of 100 memoty alloaction.

apart from the above is any more diffrence between them ??

Point 3.

This may be looks off topic to you but I have one thing to ask
is there any diffrence between the malloc and new ??

Point 4.

why we need to derefrence the pointers once we are are done with our
work; I am not aware of garbage collection, And where I can find the
memory leak into the program ?

Thanks In Advance
Regards
Ranjeet

> --
> "If you want to post a followup via groups.google.com, don't use
> the broken "Reply" link at the bottom of the article. Click on
> "show options" at the top of the article, then click on the
> "Reply" at the bottom of the article headers." - Keith Thompson

.



Relevant Pages

  • Querry (Newbe)
    ... If we do the realloc then it means that we have allocated the ... extended memory for the current memory, ... what is the diffrence between the callocand malloc() ... malloc allocates continious memeory i.e one Block while ...
    (comp.lang.c)
  • Re: realloc() implicit free() ?
    ... > now if i allocate the memory with the malloc like ... > we have to realloc with 20 ... A successful call to mallocor callocallocates a contiguous block ... char *ptr; ...
    (comp.lang.c)
  • Re: malloc/free question
    ... I'd like to be able to malloc a block of memory, ... For example, malloc 100 ... Use realloc. ... seriously consider using two lists. ...
    (comp.lang.c)
  • Re: [OT]Re: malloc
    ... so all malloc calls *always* return a pointer to memory. ... realloc() or reallocfwill be ... initialization only happens once for each byte, ...
    (comp.lang.c)
  • Re: malloc/free question
    ... I'd like to be able to malloc a block of memory, ... For example, malloc 100 ... Use realloc. ... guaranteed that a reallocrequesting a smaller size will succeed; ...
    (comp.lang.c)