From: sdarbha@ha17.eng.ua.edu (Subrahmanyam Darbha)
Subject: Difference between malloc and calloc
Originator: sdarbha@ha17.eng.ua.edu
Organization: The University of Alabama, Tuscaloosa
Julieline: see if this comes up.
Date: Mon, 31 Aug 1992 23:32:58 GMT

Recently I posted a question on this newsgroup about the following questions
The following represents a summary of the answers I received
Thanks to all who replied


Question :
I have a problem understanding the difference between calloc and malloc
because in my code malloc seems to be working and calloc does not seem to be
Can anybody enlighten me on this ?

Answers :
I had this problem one. It can be cured by a) making sure you
have included the necessary prototypes (if using ANSI C) or
b) type-casting the arguments to calloc to size_t.

    Read the manual.  The only two differences between malloc() and
calloc() are:

        1) malloc() takes a number of bytes, where calloc() takes
           a number of blocks, and the byte size of each block; and
        2) calloc() zeroes out the memory.

    So,

            p = calloc(nblocks, blocksize);

is identical to

            p = malloc(nblocks * blocksize);
            memset(p, 0, nblocks * blocksize);

    If malloc() works and calloc() doesn't, all I can figure is that you
aren't passing two arguments.  (Always use lint if you've got it, and
turn all warnings on when you compile.  Always.)  The other option is,
the code depends on the memory being non-zero.

The only difference between malloc() and calloc() is that calloc() 
initializes the memory pointed to by NULLs. In fact, calloc() actually
uses malloc() to get the memory, and then does a memset() on it.

	void *calloc(size_t nmemb, size_t size)
		allocates space for an array of nmemb objects, each of whose
		size is size. The space is initialized to all bits zero.
	void *malloc(size_t size)
		allocates space for an object whose size is specified by size.

so the difference is really that calloc reserves space for something like

	yourtype something[nmemb];

where sizeof(yourtype) == size, and also clears this space (about
nmemb*size, but may be larger), while malloc reserves space for
something like

	yourtype something;

where sizeof(yourtype) == size (and no clearing). The general consensus
is that you can use calloc but it often isn't worth the trouble, so
malloc is used (this is also more of a standard function as well).

That seems a rather significant difference ... :-)

malloc() just allocates memory, while calloc() also does the
equivalent of a memset(ptr, 0, size_of_allocated_memory);

Unless there's an outright bug in calloc, the only problem I can think
of is that calloc may clear the memory, but not necessarily initialize
fields of structs to 0. This is probably true for floats and doubles,
and may be true for pointers as well.

>Can anybody enlighten me on this ?

For further enlightenment, you may have to post the code you think
isn't working, together with a description of what you think it should
do.

If you're working in a particular environment -- say, MSDOS -- there
are other restrictions, such as the 64Kb segment restriction, to be
aware of. The system-specific programming groups may be of more help
here.

Hope this is of any help,
-- 
*************************** Who else But Darbha ******************************* 
sdarbha@buster.eng.ua.edu     Res: (205) 349 - 5143   Off: (205) 348 - 9289
*******************************************************************************