NVIDIA interview question
Which will exit/hang faster and why:
while(1)
int ptr = (int*)malloc(100);
while(1){
int ptr = (int*)malloc(100);
memset(ptr, 1, 100);
}
Interview Answers
Second one hang faster, because in first one it just allocate memory in virtual address space, whereas in second one it also allocate in physical memory.
you can allocate as much memory as you want. There is a rumor that "malloc never fails". the thing is malloc doesn't actually map physical frames during allocation. It just return the virtual address to start of the memory in virtual address space.
But when you do memset(), we require physical page frames - it is when system can hang or slow-down due to memory constraints. As underneath OS will try its best to allocate pages based on demand paging and will do numerous swap-in / swap-out of the pages to make a room for this operation in physical RAM. It will first slow down the machine, and eventually hang at later stage.
second one while(1){ int ptr = (int*)malloc(100); memset(ptr, 1, 100); } has higher probability to crash/exit as the first one is allocating memory only whereas in second program is trying to access the memory