I'm trying to call malloc()
in an infinite loop. This might sound weird, but I actually wanted to experiment with what would happen if I did this.
My main method:
void main() {
while (true) {
int64_t* ptr = malloc(sizeof(int64_t));
if (ptr == NULL) {
printf("Out of memory\n");
break;
}
}
}
malloc()
is supposed to return NULL
if OS refuses to allocate more memory. When I run this program, my PC shows that memory usage is increasing even maximum allowed page file memory is committed. But the program doesn't show that "Out of memory" on the console and the operating system crashes with the Blue Screen showing the error CRITICAL PROCESS DIED
.
I understand that this crash is caused by my program. But my question is why malloc
didn't return NULL
if the OS is running out of memory...
I compiled this program using MSVC, which comes with VS 2022.