It\'s very bothersome for me to write calloc(1, sizeof(MyStruct))
all the time. I don\'t want to use an idea like wrapping this method and etc. I mean I want to
Historical reasons.
At the time of when calloc
was introduced, the malloc
function didn't exist and the calloc
function would provide the correct alignment for one element object.
When malloc
was introduced afterwards, it was decided the memory returned would be properly aligned for any use (which costs more memory) and so only one parameter was necessary. The API for calloc
was not changed but calloc
now also returns memory properly aligned for any use.
EDIT:
See the discussion in the comments and the interesting input from @JimBalter.
My first statement regarding the introduction of malloc
and calloc
may be totally wrong.
Also the real reasons could also be well unrelated to alignment. C history has been changed a lot by compiler implementers. malloc
and calloc
could come from different groups / compilers implementers and this would explain the API difference. And I actually favor this explanation as the real reason.
calloc
allocates an array of elements in memory the size of Number of elements (first parameter) times the size of the element to allocate specified number of (second parameter). Additionally calloc
initializes this array to 0. The size of the element may be bigger than a byte and the function calculates the required memory size for you.
malloc
takes a single parameter that allocates specified number of bytes and gives you a pointer to that. Additionally, malloc
does not initialize this block to anything.
Both functions essentially gives you the same functionality.
Here's the references:
malloc calloc
The only reason I could come up with is that
int *foo = calloc(42, sizeof *foo);
is one character shorter than
int *foo = malloc(42 * sizeof *foo);
The real reason is apparently lost to the millennia centuries decades of C history and needs a programming language archaeologist to unearth, but might be related to the following fact:
In contrast to malloc()
- which needs to return a memory block aligned in accordance to the full block size - when using calloc()
as intended, the memory block would only need to be aligned in accordance to the size passed as second argument. However, the C standard forbids this optimization in conforming implementations.
it is just by design.
you could write your own calloc
void *mycalloc(size_t num, size_t size)
{
void *block = malloc(num * size);
if(block != NULL)
memset(block, 0, num * size);
return block;
}
You shouldn't allocate objects with calloc (or malloc or anything like that). Even though calloc zero-initializes it, the object is still hasn't been constructed as far as C++ is concerned. Use constructors for that:
class MyClass
{
private:
short m_a;
int m_b;
long m_c;
float m_d;
public:
MyClass() : m_a(0), m_b(0), m_c(0), m_d(0.0) {}
};
And then instantiate it with new
(or on the stack if you can):
MyClass* mc = new MyClass();