LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-02-2007, 07:10 AM   #1
aaratik
LQ Newbie
 
Registered: May 2007
Posts: 4

Rep: Reputation: 0
malloc vs calloc


Hi,

I know the basic difference between malloc and calloc i.e the memory allocated with malloc is not initialized while with calloc its initialized to zero.

why there are 2 different functions as malloc and calloc if we can get the same functionality as calloc with malloc and memset?

Also I have observed that, in programes most of the time we allocate memory dynamically using malloc.

I would like to know where exactely we will use calloc instead of malloc? any particular application?
 
Old 05-02-2007, 07:37 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
clearly not a member intro. moved to programming.
 
Old 05-02-2007, 07:44 AM   #3
Dox Systems - Brian
Member
 
Registered: Nov 2006
Posts: 344

Rep: Reputation: 31
I use calloc when I would otherwise have to use malloc and memset... You're question strikes me as being like asking "why is there a command to print strings when we can just loop through and print characters"...
 
Old 05-02-2007, 11:01 AM   #4
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
Sometimes you'll be writing information before you read it, in which case using calloc() causes unnecessary memory writing. For example, if I want to copy a string into a chunk of new address space, all I need to do is malloc() and then write the string in. If I use calloc() instead, it will zero out the new space, which is not needed because I'm just going to overwrite it anyway. Remember that a lot of these functions (especially in C) come from a time where execution speed was very important. Re-writing hundreds of kilobytes of memory unnecessarily was a huge waste of time.

If you think that's weird, you should try Perl: "There's more than one way to do it!"
 
Old 05-02-2007, 11:34 AM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by taylor_venable
Remember that a lot of these functions (especially in C) come from a time where execution speed was very important. Re-writing hundreds of kilobytes of memory unnecessarily was a huge waste of time.
Indeed.

Execution time is IMHO still important, and malloc is still far superior to calloc as it takes virtually no time to execute while calloc can be resource demanding.
You can malloc whatever size you want in far less than a microsecond, while "callocating" memory will take something like 300 milliseconds per gigabyte (YMMV), and much more if you are short of RAM and start swapping.
 
Old 05-02-2007, 11:25 PM   #6
aaratik
LQ Newbie
 
Registered: May 2007
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks for the reply.
still 1 doubt in malloc and calloc...

I read some where that memory allocated by malloc is not contiguous while with calloc its contiguous. or vice a versa.
I am not sure.

but is it true?
 
Old 05-03-2007, 08:37 AM   #7
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
a chunk of allocated memory is always logically contiguous. There's lots of fun stuff that the OS does behind your back with Virtual Memory that means that an allocated chunk of memory may not be contiguous physically, or even in memory at all!
 
Old 05-04-2007, 03:20 AM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
With malloc not only the allocated memory won't be in RAM at all at the time of allocation, but with Linux the returned pointer may even point to memory that neither exist in virtual memory. This is known as over-committing memory.
 
Old 05-04-2007, 04:32 AM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
calloc is probably implemented using malloc.
the memory from each call will of course, from your point of view, be contiguous, it would be useless otherwise. Each byte byte will be accesed as next to it's neighbour regardless of where it lies in reality, on disk or RAM or cache.
That is the operating system's problem, not yours.
 
Old 05-04-2007, 06:50 AM   #10
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
My point is that the next byte may lie neither in RAM, disk or cache with Linux and other O/Ses allowing over-commiting.

If you are sure you'll use all of the memory you allocate, that make sense to call the slower calloc, as you'll know in the first place the virtual memory you are asking for actually exists, and the time lost during the calloc will be compensated by the time saved when filling the memory with your data.
 
Old 05-04-2007, 07:07 AM   #11
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
ok Jilliagre was not bumping you, was just trying to simplify for the OP
 
Old 05-04-2007, 08:17 AM   #12
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
No problem. Thanks
 
Old 05-04-2007, 10:46 AM   #13
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
ISO C 99 says this:
Quote:
The order and contiguity of storage allocated by successive calls to the
calloc, malloc, and realloc functions is unspecified. The pointer returned
if the allocation succeeds is suitably aligned so that it may be assigned
to a pointer to any type of object and then used to access such an object
or an array of such objects in the space allocated (until the space is
explicitly deallocated). The lifetime of an allocated object extends from
the allocation until the deallocation. Each such allocation shall yield a
pointer to an object disjoint from any other object. The pointer returned
points to the start (lowest byte address) of the allocated space. If the
space cannot be allocated, a null pointer is returned. If the size of the
space requested is zero, the behavior is implementation-defined: either a
null pointer is returned, or the behavior is as if the size were some
nonzero value, except that the returned pointer shall not be used to access
an object.
 
Old 05-04-2007, 06:33 PM   #14
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by jim mcnamara
ISO C 99 says this:
...
If the space cannot be allocated, a null pointer is returned.
...
This is where Linux (and some commercial Unix-es too) are borderline with the standard.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
calloc failures easykillsoft Linux - Software 2 11-19-2005 06:02 PM
calloc returns weird address? mvorlov Programming 2 11-09-2005 07:55 AM
malloc .vs. calloc thelonius Programming 5 10-28-2005 04:50 PM
C-Program: calloc() and free() using in while xxfunkxx Programming 10 12-06-2004 09:04 PM
what is the point of calloc? Berhanie Programming 8 07-17-2004 01:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration