The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
questions in memset arunkumar_mca UNIX for Dummies Questions & Answers 7 08-09-2007 09:08 AM
about memset fuction ranj@chn High Level Programming 3 01-31-2006 04:59 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-16-2008
Registered User
 

Join Date: Jul 2008
Posts: 7
memset vs calloc

Dear Friends,
Can any one tell me the difference between memset and calloc function in C.

Regards,
Selvi
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 07-16-2008
Registered User
 

Join Date: Sep 2007
Location: Koblenz, Germany
Posts: 597
Uh oh... best being moved/posted in High level programming subforum next door I guess (if no answer comes).
Reply With Quote
  #3 (permalink)  
Old 07-16-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 500
memset() initializes byte strings to a user supplied value that is restricted to the unsigned char range. It does not allocate any memory.

calloc() allocates memory of any type (char, int, struct) and initializes that storage to zeros.

Both initialize a given storage area [memset() being the more exclusive of the two functions] but only one allocates memory for storage of a particular object type.
Reply With Quote
  #4 (permalink)  
Old 07-16-2008
Registered User
 

Join Date: Jun 2008
Posts: 24
calloc() is usually used to create arrays. It's sort of like malloc() in that it allocates memory, but calloc() as shamrock points out sets the allocated memory to 0.

The effect of calloc() is the same as doing malloc() followed by memset().
Reply With Quote
  #5 (permalink)  
Old 07-16-2008
Registered User
 

Join Date: Jul 2008
Posts: 7
Dear Friends,
Thank you for your reply.
Actually when I was reading the code of the traceroute I saw the following statement.

outip = (struct ip *)malloc((unsigned)packlen);
if (outip == NULL) {
Fprintf(stderr, "%s: malloc: %s\n", prog, strerror(errno));
exit(1);
}
memset((char *)outip, 0, packlen);

outip is of datatype struct ip *.
packlen is the size of the packet (i.e, 40 bytes).


My doubt is why here they not using the calloc function.

Regards,
Selvi.
Reply With Quote
  #6 (permalink)  
Old 07-17-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,522
That is older C code (because modern malloc does not anbd should be cast).

I'm guessing:
Some older calloc implementations had problems actually zeroing out the allocated memory. So maybe the coder knew this was a problem.

Plus, calloc does call an equivalent of memset anyway - so it is up to you to choose which one to use: malloc + memset or calloc. I personally seldom use calloc.
Reply With Quote
  #7 (permalink)  
Old 07-17-2008
Registered User
 

Join Date: Jun 2008
Posts: 24
The reason is that calloc() implies array access. They are not treating the memory area as an array here so use malloc(). calloc() is rarely used it seems to me, and in this case would not make any sense to use. Think about it, what would you make an array of in this case, what type?
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 08:42 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0