472,352 Members | 1,505 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,352 software developers and data experts.

difference between malloc and calloc?

hai to everybody,
i am having doubt in difference between the malloc
and calloc function.
please tell where to use and when to use those functions?

Nov 19 '05 #1
8 11056
venkatesh <pv***********@gmail.com> schrieb:
hai to everybody,
i am having doubt in difference between the malloc
and calloc function.
please tell where to use and when to use those functions?


malloc takes one parameter (number of chars to allocate) and
does not initialize the allocated memory, whereas calloc takes
two parameters (number of elements and number of chars per
element) and initialises the allocated memory to zero.

Examples:

// This allocats 500 ints, it does not initialize the memory:
int *m = malloc(500*sizeof(int));

// This, too, allocates 500 ints, but initializes the memory
// to zero:
int *c = calloc(500,sizeof(int));

hth.

Markus
Nov 19 '05 #2
venkatesh wrote:
hai to everybody,
i am having doubt in difference between the malloc
and calloc function.
please tell where to use and when to use those functions?


Markus Becker has explained the difference. The second
part of your question is about which function to use under
what circumstances, and my suggestion is "Use malloc() almost
always and calloc() almost never."

The reason is that the initialization to zero that calloc()
performs is usually not very helpful:

- The initialization to "all-bits-zero" is not necessarily
the same as initialization to "all-data-zero." C says
very little about the representation of values in memory,
nothing at all for floating-point or pointer values. On
many machines all-bits-zero representations will in fact
correspond to f.p. zeroes or null pointers, but this is
not guaranteed by the language and there have been machines
where the correspondence did not hold. If you get in the
habit of using calloc() to initialize f.p. and pointer
items, you may be heading for trouble.

- Usually, one allocates a chunk of dynamic memory in order
to store something in it -- and when you store something
in it, you'll overwrite whatever was there before. Thus,
the initialization performed by calloc() is usually not
needed anyhow. There are occasional exceptions where all-
bits-zero initialization is helpful, but they are unusual.
Not unheard-of, I grant, but unusual.

There's one possible advantage I can imagine for calloc() over
malloc(), and that's the opportunity for a tiny bit of sanity-
checking. Here are two ways you might try to allocate memory to
hold N items of SomeType:

SomeType *p = malloc(N * sizeof *p);
SomeType *q = calloc(N, sizeof *q);

Now, if N is so large that multiplying it by sizeof(SomeType)
exceeds the valid range of size_t, the argument in the first
form will "wrap around" and you'll silently request less memory
than you wanted; if the request succeeds you'll proceed merrily
along and try to store N items in too small a space, with the
usually unhappy and sometimes baffling consequences. The second
form, however, will fail and return NULL so your program will be
alerted that the space was not available; there'll be no silent
error. However, this seems to me to be a very small advantage,
so I'll stick with my original suggestion: malloc() almost always,
calloc() almost never.

--
Eric Sosman
es*****@acm-dot-org.invalid

Nov 19 '05 #3
Eric Sosman wrote:
There's one possible advantage I can imagine for calloc() over
malloc(), and that's the opportunity for a tiny bit of sanity-
checking. Here are two ways you might try to allocate memory to
hold N items of SomeType:

SomeType *p = malloc(N * sizeof *p);
SomeType *q = calloc(N, sizeof *q);

Now, if N is so large that multiplying it by sizeof(SomeType)
exceeds the valid range of size_t, the argument in the first
form will "wrap around" and you'll silently request less memory
than you wanted; if the request succeeds you'll proceed merrily
along and try to store N items in too small a space, with the
usually unhappy and sometimes baffling consequences. The second
form, however, will fail and return NULL so your program will be
alerted that the space was not available; there'll be no silent
error. However, this seems to me to be a very small advantage,
so I'll stick with my original suggestion: malloc() almost always,
calloc() almost never.


Does this mean that

void *my_calloc1(size_t a, size_t b)
{
void *p = malloc(a * b);
if(p) memset(p, 0, a * b);
return p;
}

would not be a valid implementation of calloc, because of the
possibility of overflow?

If calloc needs to check for overflow in a * b, how should it do so?

void *my_calloc2(size_t a, size_t b)
{
void *p = NULL;
size_t n = a * b;
if(a && b && n / a == b && n / b == a)
{
p = malloc(n);
if(p) memset(p, 0, n);
}
return p;
}

Perhaps this is overkill...

--
Simon.
Nov 19 '05 #4
Simon Biber wrote:

Eric Sosman wrote:
There's one possible advantage I can imagine for calloc() over
malloc(), and that's the opportunity for a tiny bit of sanity-
checking. Here are two ways you might try to allocate memory to
hold N items of SomeType:

SomeType *p = malloc(N * sizeof *p);
SomeType *q = calloc(N, sizeof *q);

Now, if N is so large that multiplying it by sizeof(SomeType)
exceeds the valid range of size_t, the argument in the first
form will "wrap around" and you'll silently request less memory
than you wanted; if the request succeeds you'll proceed merrily
along and try to store N items in too small a space, with the
usually unhappy and sometimes baffling consequences. The second
form, however, will fail and return NULL so your program will be
alerted that the space was not available; there'll be no silent
error. However, this seems to me to be a very small advantage,
so I'll stick with my original suggestion: malloc() almost always,
calloc() almost never.
Does this mean that

void *my_calloc1(size_t a, size_t b)
{
void *p = malloc(a * b);
if(p) memset(p, 0, a * b);
return p;
}

would not be a valid implementation of calloc, because of the
possibility of overflow?


I don't know.
If calloc needs to check for overflow in a * b, how should it do so?

void *my_calloc2(size_t a, size_t b)
{
void *p = NULL;
size_t n = a * b;
if(a && b && n / a == b && n / b == a)
{
p = malloc(n);
if(p) memset(p, 0, n);
}
return p;
}

Perhaps this is overkill...


I think calloc(0) should return the same value as malloc(0),
whatever that may be.

And I think that only one of these checks is needed:
n / a == b && n / b == a

void *my_calloc3(size_t a, size_t b)
{
void *p;
size_t n;

if (a == 0 || b == 0) {
p = malloc(0);
} else {
n = a * b;
if (n / a == b) {
p = malloc(n);
if (p != NULL) {
memset(p, 0, n);
}
} else {
p = NULL;
}
}
return p;
}

--
pete
Nov 20 '05 #5
Simon Biber wrote:
Eric Sosman wrote:
There's one possible advantage I can imagine for calloc() over
malloc(), and that's the opportunity for a tiny bit of sanity-
checking. Here are two ways you might try to allocate memory to
hold N items of SomeType:

SomeType *p = malloc(N * sizeof *p);
SomeType *q = calloc(N, sizeof *q);

Now, if N is so large that multiplying it by sizeof(SomeType)
exceeds the valid range of size_t, the argument in the first
form will "wrap around" and you'll silently request less memory
than you wanted; if the request succeeds you'll proceed merrily
along and try to store N items in too small a space, with the
usually unhappy and sometimes baffling consequences. The second
form, however, will fail and return NULL so your program will be
alerted that the space was not available; there'll be no silent
error. However, this seems to me to be a very small advantage,
so I'll stick with my original suggestion: malloc() almost always,
calloc() almost never.

Does this mean that

void *my_calloc1(size_t a, size_t b)
{
void *p = malloc(a * b);
if(p) memset(p, 0, a * b);
return p;
}

would not be a valid implementation of calloc, because of the
possibility of overflow?


Yes, I think it would be invalid. The Standard says (in
7.20.3.1)

The calloc function allocates space for an array of
/nmemb/ objects, each of whose size is /size/. [...]

This is subject to the general condition in 7.20.3

[...] If the space cannot be allocated, a null pointer
is returned. [...]

Thus, if calloc() cannot find sufficient space for /nmemb/
objects of /size/ bytes each, it must return NULL. There is no
special dispensation for overflow of nmemb * size; there is just
the requirement for a NULL returned value.
If calloc needs to check for overflow in a * b, how should it do so?


The response by "pete" seems to cover what's needed.

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 20 '05 #6
pete wrote:

void *my_calloc3(size_t a, size_t b)
{
void *p;
size_t n;

if (a == 0 || b == 0) {
p = malloc(0);
} else {
n = a * b;
if (n / a == b) {
To be pedantic, size_t may theoretically promote to int, which can
overflow.

if (a <= ((size_t) -1) / b) {
n = a * b;
p = malloc(n);
if (p != NULL) {
memset(p, 0, n);
}
} else {
p = NULL;
}
}
return p;
}


--
Peter

Nov 20 '05 #7
On 2005-11-20, Peter Nilsson <ai***@acay.com.au> wrote:
pete wrote:

void *my_calloc3(size_t a, size_t b)
{
void *p;
size_t n;

if (a == 0 || b == 0) {
p = malloc(0);
} else {
n = a * b;
if (n / a == b) {


To be pedantic, size_t may theoretically promote to int, which can
overflow.


Eh? If size_t can promote to int, how can it overflow it?
Nov 20 '05 #8
Jordan Abel wrote:

On 2005-11-20, Peter Nilsson <ai***@acay.com.au> wrote:
pete wrote:

void *my_calloc3(size_t a, size_t b)
{
void *p;
size_t n;

if (a == 0 || b == 0) {
p = malloc(0);
} else {
n = a * b;
if (n / a == b) {


To be pedantic, size_t may theoretically promote to int, which can
overflow.


Eh? If size_t can promote to int, how can it overflow it?


I suppose if 'a' was lower ranking than int,
like unsigned short or unsigned char,
and the max value for that type was equal to INT_MAX,
and 'a' was equal to INT_MAX and 'b' was greater than 1,
or any other combination where the product of a*b exceded INT_MAX,
then a*b would overflow.

If a byte has enough bits to represent all the positive
values within the range of int,
then an allowable way to implement an int,
is to make sizeof(int) equal to 2,
and use the high order byte just for the sign bit.

I never heard of a size_t lower ranking than unsigned.
It would be strange, but I think it's allowed.

--
pete
Nov 20 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: rihad | last post by:
To make up a proper dynamically allocated empty C string, would it suffice to say: return calloc(1, 1); or am I better off using the longer...
14
by: Rahul Gandhi | last post by:
Which one is more fast? malloc followed by memset or calloc
27
by: MK | last post by:
I am a newbie. Please help. The following warning is issued by gcc-3.2.2 compiler (pc Linux):...
15
by: Mohanasundaram | last post by:
Hi All, What is the difference between malloc and calloc other than the point that calloc will initialize the memory to all zeros? This was an...
37
by: Harsimran | last post by:
Can any one explain what are far pointers and what is the difference between malloc and calloc .Which is better ?
11
by: lohith.matad | last post by:
Hi all, Though the purpose of both malloc() and calloc() is the same, and as we also know that calloc() initializes the alloacted locations to...
6
by: mthread | last post by:
Hi, I am learning C++ and I have been told that an object can be created either by using calloc or new. If it is true, can some one tell me what...
13
by: manish sahu | last post by:
difference between calloc() and malloc()
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.