about malloc and calloc

R

Roka100

Hi,
I see calloc() can do malloc() and initial to value 0 , I wonder if
that means I can ALWAYS use calloc instead of malloc() ?

thanks;
 
D

Default User

Hi,
I see calloc() can do malloc() and initial to value 0 , I wonder if
that means I can ALWAYS use calloc instead of malloc() ?

You "can". It's not true that you "should" always use it.



Brian
 
K

Keith Thompson

I see calloc() can do malloc() and initial to value 0 , I wonder if
that means I can ALWAYS use calloc instead of malloc() ?

You can, but there's not much reason to do so. Initializing allocated
memory to all-bits-zero is not usually what you want to do. For
example, this does *not* guarantee that pointers or floating-point
variables will be set to NULL or 0.0, respectively.
 
K

Kenneth Brody

Keith said:
You can, but there's not much reason to do so. Initializing allocated
memory to all-bits-zero is not usually what you want to do. For
example, this does *not* guarantee that pointers or floating-point
variables will be set to NULL or 0.0, respectively.

Or, you may not require initialized memory in the first place, as you are
going to store info into it before using it. (For example, making a copy
of a string.)

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,079
Latest member
ElidaWarin

Latest Threads

Top