calloc and malloc function

Reply

Join Date: Jun 2008
Posts: 9
Reputation: rocky2008 is an unknown quantity at this point 
Solved Threads: 0
rocky2008 rocky2008 is offline Offline
Newbie Poster

calloc and malloc function

 
0
  #1
May 27th, 2009
can anyone please tell me the function of calloc() and malloc() and the difference between them ...

what does (char*)malloc(n) mean ??
Regards,
Rocky.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: calloc and malloc function

 
0
  #2
May 27th, 2009
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).
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,580
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 199
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: calloc and malloc function

 
0
  #3
May 27th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: calloc and malloc function

 
0
  #4
May 27th, 2009
From the C Standard:
  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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 9
Reputation: rocky2008 is an unknown quantity at this point 
Solved Threads: 0
rocky2008 rocky2008 is offline Offline
Newbie Poster

Re: calloc and malloc function

 
0
  #5
May 27th, 2009
what do you mean in the last line ? can u please explain ?


Originally Posted by BestJewSinceJC View Post
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.
Regards,
Rocky.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,660
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 723
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: calloc and malloc function

 
2
  #6
May 27th, 2009
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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,361
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 241
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: calloc and malloc function

 
0
  #7
May 27th, 2009
An obligatory link to http://c-faq.com/malloc/calloc.html
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC