473,386 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

malloc and calloc

Which one is more fast?
malloc followed by memset
or
calloc
Nov 14 '05 #1
14 6471
On Mon, 02 Feb 2004 02:41:31 -0800, Rahul Gandhi wrote:
Which one is more fast?
malloc followed by memset
or
calloc


That would depend on the plattform. If there is one it probably won't be
big enough to matter. If you think it would then it's time for you to make
measurements on your target platform.

I wouldn't be surprised to see calloc implemented to just call malloc and
then memset.

--
NPV

"the large print giveth, and the small print taketh away"
Tom Waits - Step right up

Nov 14 '05 #2
"Rahul Gandhi" <ga*********@hotmail.com> wrote in message
news:26**************************@posting.google.c om...
Which one is more fast?
malloc followed by memset
or
calloc


Red functions tend to go faster. But you'll have to do some profiling to
determine the appropriate chromodynamic hue of each function.

I.e. the standard doesn't impose speed requirements on functions. It's a
'Quality of Implementation' issue.

--
Peter
Nov 14 '05 #3
ga*********@hotmail.com (Rahul Gandhi) wrote in message news:<26**************************@posting.google. com>...
Which one is more fast?
malloc followed by memset
or
calloc


It is platform dependent.

But do you really care about the speed of malloc? If you do you have
a part of your program that's not written very well.

You will also find that it's quite common for 'free' to be by far the
slowest.
Nov 14 '05 #4
On 2 Feb 2004, Rahul Gandhi wrote:
Which one is more fast?
malloc followed by memset
or
calloc


Try to implement
a) malloc using calloc
b) calloc using malloc
...and you'll see how much reason there is for either to be slower
than another.

Then it's up to you to decide whether your focus is reasonable and
sane implementations. The standard doesn't mandate that. On
DeathStation you might have to put ifdefs depending on whether you
want less parallel throughput (several bytes/s for HPCSRNG) or
more latency (sometimes days when waiting for other callocs to
group with).

Nov 14 '05 #5
Rob Thorpe wrote:
ga*********@hotmail.com (Rahul Gandhi) wrote
Which one is more fast?
malloc followed by memset
or
calloc


It is platform dependent.

But do you really care about the speed of malloc? If you do you
have a part of your program that's not written very well.

You will also find that it's quite common for 'free' to be by far
the slowest.


That will normally be because the system is searching for adjacent
areas to combine, which may well be O(n) where n is the count of
allocations and free blocks. This makes freeing a large
collection of items O(sqr(N)). It is shown up quite dramatically
in the test suite for hashlib, which in turn was the inspiration
for nmalloc for the DJGPP system, where those two operations
become O(1) and O(n) respectively. Both can be found at:

<http://cbfalconer.home.att.net/download/>

(nmalloc is freely available under the DJGPP license. Hashlib is
under GPL).

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #6
On Mon, 2 Feb 2004 22:04:31 +1100, "Peter Nilsson" <ai***@acay.com.au>
wrote:
"Rahul Gandhi" <ga*********@hotmail.com> wrote in message
news:26**************************@posting.google. com...
Which one is more fast?
malloc followed by memset
or
calloc
Red functions tend to go faster. But you'll have to do some profiling to
determine the appropriate chromodynamic hue of each function.


But the blue ones are more critical, because they're coming toward
you.
I.e. the standard doesn't impose speed requirements on functions. It's a
'Quality of Implementation' issue.


--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 14 '05 #7
In <26**************************@posting.google.com > ga*********@hotmail.com (Rahul Gandhi) writes:
Which one is more fast?
malloc followed by memset
or
calloc


In principle, calloc could be implemented as malloc followed by memset.

However, it could skip the memset call if the allocated memory came
directly from the OS (rather than being "recycled"), because many OSs
clear themselves the memory they allocate to a program (for obvious
security reasons).

So, a calloc call need not be slower than a malloc plus a memset call,
but it could be faster. I was deliberately ignoring the cost of the
additional multiplication calloc has to perform (due to its interface).

However, this should NOT be the criterion for choosing one or another.
If your application needs zeroed memory, call calloc, otherwise call
malloc.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #8
CBFalconer <cb********@yahoo.com> wrote in message news:<40***************@yahoo.com>...
Rob Thorpe wrote:
ga*********@hotmail.com (Rahul Gandhi) wrote
Which one is more fast?
malloc followed by memset
or
calloc
It is platform dependent.

But do you really care about the speed of malloc? If you do you
have a part of your program that's not written very well.

You will also find that it's quite common for 'free' to be by far
the slowest.


That will normally be because the system is searching for adjacent
areas to combine, which may well be O(n) where n is the count of
allocations and free blocks. This makes freeing a large
collection of items O(sqr(N)).


Interesting, I've never actually looked at the time order of this. I
just read the code to some malloc's once and noticed that free
obviously did more work
It is shown up quite dramatically
in the test suite for hashlib, which in turn was the inspiration
for nmalloc for the DJGPP system, where those two operations
become O(1) and O(n) respectively. Both can be found at:

<http://cbfalconer.home.att.net/download/>

(nmalloc is freely available under the DJGPP license. Hashlib is
under GPL).


I didn't know that, but I can see the sense in "shifting the work to
the other side" in some cases. There are similar tradeoffs in GCs.
Nov 14 '05 #9

"Dan Pop" <Da*****@cern.ch> wrote in message
However, this should NOT be the criterion for choosing one or
another.
If your application needs zeroed memory, call calloc, otherwise call
malloc.

I would suggest malloc() followed by memset() for all occasions.
calloc() is a trap for the unwary, since floating point types and pointers
have all bits zero for 0.0 and NULL on almost all platforms, but this isn't
guaranteed.
Of course your application won't suffer from these problems, but another
programmer looking at the code will be surprised to see a call to calloc(),
and ask himself, "does the programmer who wrote this really know what he is
doing?".
In some environments it is a fairly common thing to trawl through code
looking for allocations. Searches for "malloc" and "realloc" are
unavoidable. If you have to search for "calloc" as well then it adds a layer
of difficulty no-one needs.
calloc() is a nuisance function that should be allowed to fall into disuse.
malloc() followed by memset() indicates your intention to allocate a chunk
of memory and initialise it to all bits zero.
Nov 14 '05 #10
On Mon, 02 Feb 2004 11:03:23 -0700, in comp.lang.c , Alan Balmer
<al******@att.net> wrote:
On Mon, 2 Feb 2004 22:04:31 +1100, "Peter Nilsson" <ai***@acay.com.au>
wrote:
Red functions tend to go faster. But you'll have to do some profiling to
determine the appropriate chromodynamic hue of each function.


But the blue ones are more critical, because they're coming toward
you.


/really/ REALLY fast.....

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #11
In article <bv**********@newsg3.svr.pol.co.uk>,
"Malcolm" <ma*****@55bank.freeserve.co.uk> wrote:
"Dan Pop" <Da*****@cern.ch> wrote in message
However, this should NOT be the criterion for choosing one or
another.
If your application needs zeroed memory, call calloc, otherwise call
malloc.

I would suggest malloc() followed by memset() for all occasions.
calloc() is a trap for the unwary, since floating point types and pointers
have all bits zero for 0.0 and NULL on almost all platforms, but this isn't
guaranteed.


Excuse me, but malloc () + memset () has exactly the same problem!
Nov 14 '05 #12
Christian Bau <ch***********@cbau.freeserve.co.uk> writes:
In article <bv**********@newsg3.svr.pol.co.uk>,
"Malcolm" <ma*****@55bank.freeserve.co.uk> wrote:
"Dan Pop" <Da*****@cern.ch> wrote in message
However, this should NOT be the criterion for choosing one or
another.
If your application needs zeroed memory, call calloc, otherwise call
malloc.

I would suggest malloc() followed by memset() for all occasions.
calloc() is a trap for the unwary, since floating point types and pointers
have all bits zero for 0.0 and NULL on almost all platforms, but this isn't
guaranteed.


Excuse me, but malloc () + memset () has exactly the same problem!


I *think* Malcom's point is that memset() makes it more explicit that
the memory is being set to all-bits-zero, whereas calloc(), being more
implicit might lead the unwary to assume that floating-point values
are set to 0.0 and pointers to NULL.

I'm not convinced, either that Malcolm is right or that I'm correctly
guessing what he meant.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #13
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.org> wrote:
Christian Bau <ch***********@cbau.freeserve.co.uk> writes:
In article <bv**********@newsg3.svr.pol.co.uk>,
"Malcolm" <ma*****@55bank.freeserve.co.uk> wrote:
"Dan Pop" <Da*****@cern.ch> wrote in message
> However, this should NOT be the criterion for choosing one or
> another.
> If your application needs zeroed memory, call calloc, otherwise call
> malloc.
>
I would suggest malloc() followed by memset() for all occasions.
calloc() is a trap for the unwary, since floating point types and
pointers
have all bits zero for 0.0 and NULL on almost all platforms, but this
isn't
guaranteed.


Excuse me, but malloc () + memset () has exactly the same problem!


I *think* Malcom's point is that memset() makes it more explicit that
the memory is being set to all-bits-zero, whereas calloc(), being more
implicit might lead the unwary to assume that floating-point values
are set to 0.0 and pointers to NULL.

I'm not convinced, either that Malcolm is right or that I'm correctly
guessing what he meant.


Languages like Pascal did a certain amount of extra work for built-in
functions like "writeln". The C language could in principle do something
similar for calloc: Instead of calloc (nmemb, size) there could be an
alternative calloc (nmemb, type) which would allocate an array of
(nmemb) elements of the given type, each initialised like static
variables would be initialised. For example, calloc (100, double) could
with a little bit of support in the compiler allocate an array of 100
doubles, each set to 0.0.
Nov 14 '05 #14
In <ln************@nuthaus.mib.org> Keith Thompson <ks***@mib.org> writes:
I *think* Malcom's point is that memset() makes it more explicit that
the memory is being set to all-bits-zero, whereas calloc(), being more
implicit might lead the unwary to assume that floating-point values
are set to 0.0 and pointers to NULL.


Even the unwary could ask himself: how could calloc guess which bytes
are going to be used for storing floating-point values and/or pointers,
from the available information: N objects of M bytes each?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Koster | last post by:
Sorry for the re-post, but my previous question was left unanswered. I have a question about the appropriateness of calloc. Consider an array of pointers to structs which need to be allocated...
26
by: dagger | last post by:
Hi there. I'm using C under FreeBSD with the gcc compiler and am having a bit of trouble using the calloc and realloc calls. As an example the code snippet: #include <stdio.h> int main() {...
16
by: laberth | last post by:
I've got a segmentation fault on a calloc and I don'tunderstand why? Here is what I use : typedef struct noeud { int val; struct noeud *fgauche; struct noeud *fdroit; } *arbre; //for those...
27
by: MK | last post by:
I am a newbie. Please help. The following warning is issued by gcc-3.2.2 compiler (pc Linux): ================================================================== read_raw_data.c:51: warning:...
8
by: EvilRix | last post by:
I've always casted the result of a call to c/malloc viz.: int * ptr = (int *) calloc(10, sizeof(int)); However, I know this is not strictly necessary as there is always an implied cast on the...
6
by: Ramasubramanian XR (AS/EAB) | last post by:
What is diff b/w malloc,calloc,realloc could any one explain
7
by: mef526 | last post by:
I have had this problem for months now and it has been nagging me. I have a large project that has several C++ DLL's, one of them uses malloc / calloc / free for the other DLL's. I process very...
14
by: Roka100 | last post by:
Hi all, I tried 2 programs : #include <stdio.h> #include <string.h> 1, int main(void){ char *str = NULL;
47
by: Raman | last post by:
Hi All, As per my understanding, malloc and calloc differs in the way they allocate memory( malloc give a contigous block, But calloc may give Non-contigous block as well). Now consider this...
5
by: reachanil | last post by:
Hi, We've interposed malloc/calloc/realloc/memalign/valloc in our application. While all of our application calls our own implementation of these functions, there seems to be some issue with the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.