Apr 30, 2012 at 9:33am UTC
hi,
Can anyone tell me the exact difference between memset,malloc and calloc.I am new to c++, Know this is simple question to ask in c++ but i am getting lot of confusions in using them and i want to knw whr exactly i can use these.
Regards,
Sharath.
Apr 30, 2012 at 9:55am UTC
These function is mostly used in C.
memset sets the bytes in a block of memory to a specific value.
malloc allocates a block of memory.
calloc, same as malloc. Only difference is that it initializes the bytes to zero.
In C++ the preferred method to allocate memory is to use new.
C: int intArray = (int *) malloc(10 * sizeof (int ));
C++: int intArray = new int [10];
C: int intArray = (int *) calloc(10 * sizeof (int ));
C++: int intArray = new int [10]();
Apr 30, 2012 at 10:01am UTC
Thks for the quick reply....
Can u please tell me where exactly can we use these three..
for example if we use a structure wat to use to assign...
Apr 30, 2012 at 2:39pm UTC
malloc and free pair are not recommended for object mem allocation in c++
for allocating object's mem blocks , use key word new
for recycling mem blocks ,use delete correspondingly
malloc and free know nothing about class information
but new and delete do .
malloc --- simply get you some mem blocks in bytes without cleaning to zero
calloc -----get you some mem blocks that is initialized all with zero
that is the difference between them.
here is an example:
http://www.diffen.com/difference/Calloc_vs_Malloc
Last edited on Apr 30, 2012 at 2:46pm UTC
May 2, 2012 at 6:37am UTC
tks for ur reply guys....