writes:
[snip]
> 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 ??
Eric Sosman has already written most of what I would have, but I'd
like to make one additional point.
calloc initializes the allocated block to all-bits-zero. This is not
necessarily useful. If the allocated block is to be used as an array
of unsigned char, you're guaranteed that the elements of the array
will have the value 0. If it's to be used as an array of some other
integer type, you're *practically* guaranteed the same thing; the
standard doesn't explicitly state that all-bits-zero is a valid
representation for the value 0, but I think there's been a more recent
ruling from the committee that it is.
For other types (particularly floating-point and pointer types),
there's no guarantee that all-bits-zero is a valid value. On many
systems, setting a floating-point variable to all-bits-zero will
result in the value 0.0, and setting a pointer variable to
all-bits-zero will result in a null pointer, *but* this is not
guaranteed and you shouldn't assume it. It's likely to work correctly
during testing, then fail mysteriously when you port it to another
implementation and you've forgotten your initial assumptions.
If you want the allocated memory to be initialized to some meaningful
value, you should do it yourself rather than depending on calloc (or
memset, or whatever) to do it for you. There are cases where you can
depend on calloc to do the right thing, but in general it's safest to
assume that it fills the allocated block with garbage -- in which case
you might as well use malloc() rather than calloc().
--
Keith Thompson (The_Other_Keith)
kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.