As a C and C++ programmer I have used malloc and new to allocate memory. I am just wondering: how does the OS allocate memory?
D
1) Does it allocates from RAM or from hard-disk or from somewhere else?
On modern systems/platforms supporting virtual memory, the operating system decides where and how to allocate/store the memory. The system is also free to move the memory from one place to another either in ram or on disk.
2) Can I borrow memory form hard disk just in case ?
The operating system that manages memory can borrow memory from disk.
You can also explicitly borrow memory from disk by storing data in a file. C and C++ standard libraries (stdio.h and fstream) enable files manipulation.