Categories:
.NET (357)
C (330)
C++ (183)
CSS (84)
DBA (2)
General (7)
HTML (4)
Java (574)
JavaScript (106)
JSP (66)
Oracle (114)
Perl (46)
Perl (1)
PHP (1)
PL/SQL (1)
RSS (51)
Software QA (13)
SQL Server (1)
Windows (1)
XHTML (173)
Other Resources:
What is the difference between calloc and malloc? ....
What's the difference between calloc and malloc? Which should I use? Is it safe to take advantage of calloc's zero-filling? Does free work on memory allocated with calloc, or do you need a cfree?
✍: Guest
calloc(m, n) is essentially equivalent to
p = malloc(m * n);
memset(p, 0, m * n);
There is no important difference between the two other than the number of arguments and the zero fill.Use whichever function is convenient. Don't rely on calloc's zero fill too much (see below); usually, it's best to initialize data structures yourself, on a field-by-field basis, especially if there are pointer fields.
calloc's zero fill is all-bits-zero, and is therefore guaranteed to yield the value 0 for all integral types (including '\0' for character types). But it does not guarantee useful null pointer values or floating-point zero values.
free is properly used to free the memory allocated by calloc; there is no Standard cfree function.
One imagined distinction that is not significant between malloc and calloc is whether a single element or an array of elements is being allocated. Though calloc's two-argument calling convention suggests that it is supposed to be used to allocate an array of m items of size n, there is no such requirement; it is perfectly permissible to allocate one item with calloc (by passing one argument as 1) or to allocate an array with malloc (by doing the multiplication yourself; (Nor does structure padding enter into the question; any padding necessary to make arrays of structures work correctly is always handled by the compiler, and reflected by sizeof.
2016-03-16, 1307👍, 0💬
Popular Posts:
Can we use the constructor, instead of init(), to initialize servlet? Yes , of course you can use th...
Where are all .NET Collection classes located ? System.Collection namespace has all the collection c...
Can you explain project life cycle ? Figure :- 12.2 Life cycle of a project There are five stages of...
What will be printed as the result of the operation below: main() { char *ptr = " Cisco Systems"; *p...
WHat will be the result of the following code? #define TRUE 0 // some code while (TRUE) { // some co...