Advertising
c question: malloc vs calloc
Forum rules
Forum rule Nº 15 is strictly enforced in this subforum.
Forum rule Nº 15 is strictly enforced in this subforum.
c question: malloc vs calloc
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.
-
Strangelove
- Posts: 286
- Joined: Thu Nov 25, 2010 6:32 pm
Re: c question: malloc vs calloc
The main difference is that calloc() clears the allocated memory. malloc() doesn't.
malloc() is fast and unsafe, calloc() is safe but slower.
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
Re: c question: malloc vs calloc
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?
Re: c question: malloc vs calloc
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.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?
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

"just not into this RA stuffz"

"just not into this RA stuffz"
Re: c question: malloc vs calloc
m0skit0 wrote: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.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?
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.
