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, 1895👍, 0💬
Popular Posts:
Can static variables be declared in a header file? You can't declare a static variable without defin...
Can event’s have access modifiers ? Event’s are always public as they are meant to serve every one r...
How can a servlet refresh automatically if some new data has entered the database? You can use a cli...
How do we generate strong names ? or What is use the of SN.EXE ? or How do we apply strong names to ...
How To Control White Spaces between Table Cells? - XHTML 1.0 Tutorials - Understanding Tables and Ta...