memset vs calloc

Dear Friends,
Can any one tell me the difference between memset and calloc function in C.

Regards,
Selvi

Uh oh... best being moved/posted in High level programming subforum next door I guess (if no answer comes).

memset() initializes byte strings to a user supplied value that is restricted to the unsigned char range. It does not allocate any memory.

calloc() allocates memory of any type (char, int, struct) and initializes that storage to zeros.

Both initialize a given storage area [memset() being the more exclusive of the two functions] but only one allocates memory for storage of a particular object type.

calloc() is usually used to create arrays. It's sort of like malloc() in that it allocates memory, but calloc() as shamrock points out sets the allocated memory to 0.

The effect of calloc() is the same as doing malloc() followed by memset().

Dear Friends,
Thank you for your reply.
Actually when I was reading the code of the traceroute I saw the following statement.

                              outip = \(struct ip *\)malloc\(\(unsigned\)packlen\);
                              if \(outip == NULL\) \{
                                       Fprintf\(stderr, "%s: malloc: %s\\n", prog, strerror\(errno\)\);
                                       exit\(1\);
                              \} 
                              memset\(\(char *\)outip, 0, packlen\);
                 
                 outip is of datatype struct ip *.
                 packlen is the size of the packet \(i.e, 40 bytes\).
                  

      My doubt is why here they not using the calloc function.

Regards,
Selvi.

That is older C code (because modern malloc does not anbd should be cast).

I'm guessing:
Some older calloc implementations had problems actually zeroing out the allocated memory. So maybe the coder knew this was a problem.

Plus, calloc does call an equivalent of memset anyway - so it is up to you to choose which one to use: malloc + memset or calloc. I personally seldom use calloc.

The reason is that calloc() implies array access. They are not treating the memory area as an array here so use malloc(). calloc() is rarely used it seems to me, and in this case would not make any sense to use. Think about it, what would you make an array of in this case, what type?

I wanted to make some mention here regarding coding -

calloc() takes two parameters, a count and a size. It simply multiplies the two values, requests that much from the heap and then does a bzero() on the result, and then returns the beginning address of the array. The result is in fact no different from a malloc() of that same product followed by a bzero() other than the typing used for the calloc() call and for the variable to which it's return value is stored. The size value can be anything from a "char" (one byte), an int (like four bytes on a 32bit machine), or a structure (size specific to the struct declaration). Sometimes used for what we used to call a "dope vector", that is, the address of the start of an array of addresses, for example. like this:
char **cpp = (char **) calloc( 10, sizeof(char *) );
in a very simple example. The variable "cpp" then becomes the address of an array of character pointers. Check K&R's "shell sort" as a good example of how one can use such a construct.

memset() is used to set a pre-allocated block of memory to a specific value. A very different kind of thing. Useful, but to be honest I don't use it much.

These days, C++ gives us "new" and we use constructors which may clear values. One thing to remember - if you compile a "C" program with the debug flag set, variables are set to zero, even the "automatic" ones. Remove the debug flag and the automatic values are no longer set. I saw one poor fellow loose an entire day trying to figure out why his program broke with no changes when he compiled it "in production". He had failed to initialize one of his automatic variables.