943,719 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3728
  • C RSS
May 27th, 2009
0

calloc and malloc function

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

what does (char*)malloc(n) mean ??
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rocky2008 is offline Offline
9 posts
since Jun 2008
May 27th, 2009
0

Re: calloc and malloc function

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

2. (type)expression called cast. 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) means dynamically 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).
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
May 27th, 2009
0

Re: calloc and malloc function

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.
Last edited by BestJewSinceJC; May 27th, 2009 at 4:57 am.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
May 27th, 2009
0

Re: calloc and malloc function

From the C Standard:
Quote ...
  1. 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.
...
  1. 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.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
May 27th, 2009
0

Re: calloc and malloc function

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rocky2008 is offline Offline
9 posts
since Jun 2008
May 27th, 2009
2

Re: calloc and malloc function

calloc is essentially this:
  1. void *calloc ( size_t n, size_t size )
  2. {
  3. void *base = malloc ( n * size );
  4.  
  5. if ( base != NULL )
  6. memset ( base, 0, n * size );
  7.  
  8. return base;
  9. }
The difference is that the bytes are set to zero with calloc and left as-is with malloc.
Last edited by Narue; May 27th, 2009 at 10:12 am.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 27th, 2009
0

Re: calloc and malloc function

An obligatory link to http://c-faq.com/malloc/calloc.html
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Oct 26th, 2010
-1
Re: calloc and malloc function
give some example
and explain each word.
about malloc and calloc?
Reputation Points: 7
Solved Threads: 0
Newbie Poster
surendra verma is offline Offline
1 posts
since Oct 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: 2d array passing error
Next Thread in C Forum Timeline: Array Issues in 2D array





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC