Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

malloc & calloc

Tags:

c

linux

As per:

calloc(20, sizeof(int))
malloc(20 * sizeof(int))

Which will allocate memory for the 20 integers.

Does malloc() & calloc() allocates virtual or physically continuous space?

like image 952
Katoch Avatar asked Dec 09 '22 18:12

Katoch


2 Answers

C doesn't say that the machine has both physical and virtual address space.

All you know is that you get pointers, and that you can index/dereference them in a continuous fashion as defined by the language's operators.

If doing so requires the hardware to re-map virtual addresses to physical ones, or send e-mail to someone who replies with the content of the addressed location, is implementation-defined.

like image 106
unwind Avatar answered Dec 11 '22 09:12

unwind


Whether the space is physically continuous or not depends on the platform you are developing on, the MMU and the OS....

Virtually it will be continuous, always.

Whether it is calloc or malloc will not make a difference.

like image 22
Matthieu Avatar answered Dec 11 '22 08:12

Matthieu