473,569 Members | 2,573 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

malloc and calloc

Which one is more fast?
malloc followed by memset
or
calloc
Nov 14 '05 #1
14 6489
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*********@ho tmail.com> wrote in message
news:26******** *************** ***@posting.goo gle.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.

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

--
Peter
Nov 14 '05 #3
ga*********@hot mail.com (Rahul Gandhi) wrote in message news:<26******* *************** ****@posting.go ogle.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*********@hot mail.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********@yah oo.com) (cb********@wor ldnet.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*********@ho tmail.com> wrote in message
news:26******* *************** ****@posting.go ogle.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*********@hot mail.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********@yah oo.com> wrote in message news:<40******* ********@yahoo. com>...
Rob Thorpe wrote:
ga*********@hot mail.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.c h> 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

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

Similar topics

5
3630
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 space on the heap, for example: typedef struct myStruct *PMYSTRUCT; struct myStruct { int i; int j;
26
6736
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() { char *ptr;
16
8961
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 who don't speak french arbre means tree.
27
4724
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: assignment makes pointer from integer without a cast ================================================================== when the following piece of...
8
1874
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 void pointer that is returned. A potential reason for not casting was pointed out to me today by Martin Dickopp (cheers Martin). I just wondered...
6
10679
by: Ramasubramanian XR (AS/EAB) | last post by:
What is diff b/w malloc,calloc,realloc could any one explain
7
2162
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 large datafiles (100MB to 300 MB) that have more than 524,000 lines with 5 columns of numbers in text. I allocate 8 arrays with 524,000 or more...
14
7464
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
537
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 program. int main() { char *p;
5
2206
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 dl.so shared library (Windriver, ppc, linux 32 bit). The dl.so library defines a function called - _dl_tls_setup and another one -...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8130
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7677
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1223
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
940
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.