Advertising (This ad goes away for registered users. You can Login or Register)

c question: malloc vs calloc

Discuss about your favorite (gaming...or not) devices here. The most popular ones will end up getting their own categories
Programming discussions for your favorite Device
Forum rules
Forum rule Nº 15 is strictly enforced in this subforum.
Post Reply
MVP
Posts: 78
Joined: Fri Jan 21, 2011 4:21 am

c question: malloc vs calloc

Post by MVP » Fri Apr 08, 2011 2:41 am

whats the advantages/disadvantages of each? just want to get a better feel of when to use each one. it seems to me malloc is generally the way to go.
Advertising

Strangelove
Posts: 286
Joined: Thu Nov 25, 2010 6:32 pm

Re: c question: malloc vs calloc

Post by Strangelove » Fri Apr 08, 2011 3:04 am

The main difference is that calloc() clears the allocated memory. malloc() doesn't.

malloc() is fast and unsafe, calloc() is safe but slower.
Advertising
"If you have specific questions ... don't hesitate to ask as the more generic the question is the more philosophic the answer will be" - PSPWizard

MVP
Posts: 78
Joined: Fri Jan 21, 2011 4:21 am

Re: c question: malloc vs calloc

Post by MVP » Fri Apr 08, 2011 4:04 pm

so would it be safe to say use malloc if your filling the entire block allocated? and calloc if the entire block may not be filled?

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: c question: malloc vs calloc

Post by m0skit0 » Fri Apr 08, 2011 6:56 pm

MVP wrote:so would it be safe to say use malloc if your filling the entire block allocated? and calloc if the entire block may not be filled?
Not really. It's programmer duty to know what his program is doing. What are you going to check, the entire block to see if it's all zeroes? You should use another variable to indicate the status of a block (int or bool in C++), preferabily wrapped inside a struct or a class.

Anyway, when you malloc a block of memory is usually because you want to fill it, and dynamicaly allocating memory (malloc/calloc) without using it is a bit stupid IMHO. That's why it's called "dynamic", because you can reserve memory only when you need it. It doesn't really matter if you initialize it or not, except if you're using different block sizes or need explicitly to initialize the remaining part of the buffer. This last one happens on HBL here, although I used memset because calloc would be slower because of filling the whole buffer. Usually malloc + memset are the way devs go in my experience.

Maybe a scenario where calloc would be faster than malloc+memset is if you allocate a block of memory to write small-sized values at non-contiguous addresses.
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

MVP
Posts: 78
Joined: Fri Jan 21, 2011 4:21 am

Re: c question: malloc vs calloc

Post by MVP » Fri Apr 08, 2011 11:04 pm

m0skit0 wrote:
MVP wrote:so would it be safe to say use malloc if your filling the entire block allocated? and calloc if the entire block may not be filled?
Not really. It's programmer duty to know what his program is doing. What are you going to check, the entire block to see if it's all zeroes? You should use another variable to indicate the status of a block (int or bool in C++), preferabily wrapped inside a struct or a class.

Anyway, when you malloc a block of memory is usually because you want to fill it, and dynamicaly allocating memory (malloc/calloc) without using it is a bit stupid IMHO. That's why it's called "dynamic", because you can reserve memory only when you need it. It doesn't really matter if you initialize it or not, except if you're using different block sizes or need explicitly to initialize the remaining part of the buffer. This last one happens on HBL here, although I used memset because calloc would be slower because of filling the whole buffer. Usually malloc + memset are the way devs go in my experience.

Maybe a scenario where calloc would be faster than malloc+memset is if you allocate a block of memory to write small-sized values at non-contiguous addresses.

exactly what i needed clarified, thanks.

Post Reply

Return to “Programming and Security”