Click here to Skip to main content
15,881,831 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
calloc allocates num blocks of memory, each of size size:

C++
void * calloc ( size_t num, size_t size );


Allocate space for array in memory. Allocates a block of memory for an array of num elements, each of them size bytes long, and initializes all its bits to zero.

In contrast, malloc allocates one block of memory of size size:

C++
void * malloc ( size_t size );


Allocate memory block. Allocates a block of size bytes of memory, returning a pointer to the beginning of the block.

Now my questions are:

1. Is there any difference between both (except for the zero-initialization by calloc)..?

2. Does calloc() allocate only contiguous memory Or it can allocate non-contiguous also..?
Posted
Comments
Albert Holguin 17-Nov-13 18:10pm    
Not sure why you would want to allocate non-contiguous data. Remember that having data be contiguous makes handling and addressing a lot faster than non-contiguous segments.
Atul Khanduri 17-Nov-13 18:20pm    
Ya i know this but just for knowledge i asked this question.....
Albert Holguin 18-Nov-13 11:21am    
Ok, sounds good.
Atul Khanduri 18-Nov-13 13:17pm    
:-)
Albert Holguin 18-Nov-13 13:26pm    
I can both appreciate and relate to curiosity.... :)

1) No.

2) No. It's always contiguous.
 
Share this answer
 
Comments
Stefan_Lang 18-Nov-13 5:23am    
I'm not quite sure about 1: What if you want to allocate an array of structs, where each struct contains one pointer and one char? Wouldn't the alignment requirement force padding between elements? How should the malloc syntax know or guess about that with only one parameter?

I think I need to do some testing...
Freak30 18-Nov-13 7:41am    
You would obviously call malloc(sizeof(YourStruct) * numberOfElements);
The sizeof() does the padding for you.
Richard MacCutchan 18-Nov-13 8:38am    
A simple test shows that it uses the packing default as chosen by the compiler.
nv3 18-Nov-13 7:48am    
Correct on both accounts. That (2) is correct can already by seen by looking at the function signature. When only one pointer is returned, how could the allocated memory not be contiguous. My 5.
CPallini 18-Nov-13 7:53am    
5
1. Actually, the memory created by calloc () is TOUCHED by the OS and initialized with zeros. There will be some performance compared to malloc( ) where the memory pointer is being returned which indicates the start of the memory block allocated.

The memory allocated is NOT TOUCHED unless it is being used by the OS.

2. Yes the memory allocated to calloc() is always contiguous by the OS. It swaps out the memory space if required, in order to assign it to calloc().

[brydon]capitalization and spelling fixes[/brydon]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900