473,699 Members | 2,838 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
14 6507
On Mon, 02 Feb 2004 11:03:23 -0700, in comp.lang.c , Alan Balmer
<al******@att.n et> 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.c om/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**********@n ewsg3.svr.pol.c o.uk>,
"Malcolm" <ma*****@55bank .freeserve.co.u k> wrote:
"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.


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**********@n ewsg3.svr.pol.c o.uk>,
"Malcolm" <ma*****@55bank .freeserve.co.u k> wrote:
"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.


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_Keit h) 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.or g>,
Keith Thompson <ks***@mib.or g> wrote:
Christian Bau <ch***********@ cbau.freeserve. co.uk> writes:
In article <bv**********@n ewsg3.svr.pol.c o.uk>,
"Malcolm" <ma*****@55bank .freeserve.co.u k> wrote:
"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.


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.or g> Keith Thompson <ks***@mib.or g> 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
3637
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
6754
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
8982
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
4753
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 code was compiled. The offending statement is calloc. A similar statement in the main() function...
8
1882
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 what the thoughts of the group were on this. Cheers,
6
10696
by: Ramasubramanian XR (AS/EAB) | last post by:
What is diff b/w malloc,calloc,realloc could any one explain
7
2168
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 doubles , and 10 arrays of 32,768 doubles. Is there a min size for malloc / calloc required to...
14
7489
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
2211
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 - _dl_deallocate_tls. When our application that linked in -ldl and -lpthread, was shutting down, we saw a crash with...
0
8612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9171
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9032
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8880
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6532
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3053
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
3
2008
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.