The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

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 12:08 PM
about memset fuction ranj@chn High Level Programming 3 01-31-2006 08:59 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-16-2008
salvi salvi is offline
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
  #2 (permalink)  
Old 07-16-2008
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,257
Uh oh... best being moved/posted in High level programming subforum next door I guess (if no answer comes).
  #3 (permalink)  
Old 07-16-2008
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 741
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.
  #4 (permalink)  
Old 07-16-2008
osulinux osulinux is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 31
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().
  #5 (permalink)  
Old 07-17-2008
salvi salvi is offline
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.
  #6 (permalink)  
Old 07-17-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,643
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.
  #7 (permalink)  
Old 07-17-2008
osulinux osulinux is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 31
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?
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 06:52 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0