It's easier to follow the discussion if you left-justify your text.
Indented text tends to look like it's a quotation.
I got bit confused by your commnets so let me berif myself what
I said.
Suppose we have the memory of 10 bytes,
2000 to 2010;
I think you mean 2000 to 2009.
Case 1:
now if i allocate the memory with the malloc like
malloc(6), Then let say that the memroy will be allocated
from 2000 to 2005,
Now I realloc it let say,
realloc(8), Then the memory will be allocated
from the 2000 to 2007,
Now for the above case we do not need to free the memory ...right.
Case 2:
Now take the other case,
we have to realloc with 20
realloc(20); Overe here The memory is not available
so the whole chunk of memory will be allocated in the new
available place. ... right.
let say it is allocated from 5000 to 5019,
now in this case we have to free the 2000 to 2005 memory loaction.
No. This is really simpler than you're making it out to be.
A successful call to malloc() or calloc() allocates a contiguous block
of memory and returns a pointer to it. (An unsuccessful call returns
NULL.) This memory eventually needs to be deallocated.
A successful call to realloc() allocates a contiguous block of memory,
copies the contents of the old block to the new block, deallocates the
old block, and returns a pointer to the new block. (An unsuccessful
calls returns NULL and leaves the old block alone.) This is true
regardless of whether the new size is less than, equal to, or greater
than the old size. Since the old block of memory has been
deallocated, the old pointer is indeterminate; you shouldn't try to do
anything with it.
Another way of looking at this is that realloc() effectively changes
the size (and possibly the location) of an allocated object. That
interpretation has some problems, though, because it's not really the
same object; it's a new object with the contents of the old object.
As it happens, an implementation will often (but not always) be able
to optimize the allocate/copy/deallocate sequence by re-using the old
object. The circumstances in which it can do this are
implementation-specific, and you shouldn't depend on any particular
behavior. Even if the new size is the same as the old size, the
implementation might use the realloc() as an opportunity to
consolidate free space.
Here's an example. Please note that the following is simplified code,
not to be used in real life. I'm ignoring the possibility that
malloc() or realloc() could fail.
char *ptr;
ptr = malloc(1000);
/*
* We have a 1000-byte buffer.
* After a while, we need more space.
*/
ptr = realloc(ptr, 2000);
/*
* Now we have a 2000-byte buffer.
* After a while, we don't need the whole thing.
*/
ptr = realloc(ptr, 500);
/*
* Now we have a 500-byte buffer.
* Eventaully, we're finished with it.
*/
free(ptr);
As this code executes ptr could point to three different locations in
memory, or it could point to the same location each time. We don't
know, and we don't care. Each call to realloc() deallocates the old
buffer and allocates a new one (possibly in the same place). We only
need to call free() when we're done with the last allocated buffer;
the others were all deallocated by realloc().
Why is this simplified code bad? First, I didn't check whether the
initial malloc() return NULL, indicating an allocation failure; if it
does, any attempt to use the allocated block can cause bad things
to happen. Second, if the realloc() call in
ptr = realloc(ptr, 2000);
fails, realloc() returns NULL, but the previously allocated block is
left alone. By assigning the result of realloc() to ptr, I may have
destroyed my only pointer to the allocated block, creating a memory
leak. If you want to be able to recover from a realloc() failure, you
need to assign the result to a separate pointer variable which you
then check for NULL.
[...]
Joe said:
So what is the Diffrence then why specifically you need the
two arguments for this calloc() ? The block allocation
makes more sense for the two arguments passed to the
calloc (May Be I May Be Wrong Do Correct Me)
It's probably just for historical reasons. calloc() is typically used
to allocate space for an array; it's convenient to pass the element
size and the number of elements as separate arguments.