|
Not to disagree, but
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.
Last edited by fsahog; 07-17-2008 at 07:23 PM..
Reason: To make it better
|