ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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"...
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!"
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
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.
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!
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
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.
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.
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.