954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

calloc and malloc function

can anyone please tell me the function of calloc() and malloc() and the difference between them ...

what does (char*)malloc(n) mean ??

rocky2008
Newbie Poster
9 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

1. calloc, malloc, realloc et al: http://irc.essex.ac.uk/www.iota-six.co.uk/c/f7_dynamic_memory_allocation.asp (or lots of another tutorials on this topic).

2. (type)expression calledcast. It forces a conversion of expression value to the type; malloc() returns void* type value (pointer to void) - generic pointer value; char* denotes pointer to char type - so (char*)malloc(n) meansdynamically allocate n bytes and get a pointer to this memory as if there are chars into this memory block.

No need to cast void* pointer explicitly in C because void* pointer is converted to any pointer value implicitly. You must convert void* pointer to desired pointer type explicitly in C++, however no needs in malloc call in C++ (it's the other story why).

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

From what I remember, essentially, they both dynamically (while the program is running) allocate memory for your program, and the main difference is that calloc sets all the memory (that it allocates to your program) to zero's while malloc does not. So after calling malloc, the memory that was set aside for your program would still have whatever 0'1 and 1's were in the bits before, but for calloc, all those bits would be set to 0's.

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 

From the C Standard:

void *calloc(size_t nmemb, size_t size);

The calloc function allocates space for an array of nmemb objects, each of whose size is size. The space is initialized to all bits zero. Note that this need not be the same as the representation of floating-point zero or a null pointer constant.
...

void *malloc(size_t size);

The malloc function allocates space for an object whose size is specified by size and whose value is indeterminate.
None the less look at the link mentioned above.

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

what do you mean in the last line ? can u please explain ?

From what I remember, essentially, they both dynamically (while the program is running) allocate memory for your program, and the main difference is that calloc sets all the memory (that it allocates to your program) to zero's while malloc does not. So after calling malloc, the memory that was set aside for your program would still have whatever 0'1 and 1's were in the bits before, but for calloc, all those bits would be set to 0's.
rocky2008
Newbie Poster
9 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

calloc is essentially this:

void *calloc ( size_t n, size_t size )
{
  void *base = malloc ( n * size );

  if ( base != NULL )
    memset ( base, 0, n * size );

  return base;
}

The difference is that the bytes are set to zero with calloc and left as-is with malloc.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

give some example
and explain each word.
about malloc and calloc?

surendra verma
Newbie Poster
1 post since Oct 2010
Reputation Points: 7
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You