back
2 comments
This has little to to do with C language. Every programming language that allocates dynamic memory from the operating system via syscall has to deal with it.

In Linux you can use sysctl ´vm.overcommit_memory´ to make the OS do what you want. There are three options:

0 = The default is heuristic overcommit handling.

1 = Always overcommit. Good choice for many data heavy science applications that use gigantic sparse arrays.

2 = No overcommit. Only swap + tunable amount (default is 50%) of physical RAM available. Then allocation fails.

This is going to be highly dependent on the operating system, system architecture as well as the compiler and linker. But you could instrument your code to measure the time difference with and without. The malloc should result in shorter access times?