Search code examples
c++garbage-collectiondynamic-memory-allocation

What would declaring a pointer with the "new" operator within a while loop do to my computer?


So I recently got into learning C++ and I was really intrigued by memory allocation, garbage collection, heap, stack, etc...

I wrote this program, and I was wondering what it would do to my computer? Would it crash because my RAM would get filled up? Would it run slower? Would it stop before it fills up too much ram?

I am too afraid of running it on my machine.

#include <iostream>
using namespace std;

int main()
{
    while (true) {
        int* x = new int;
    }
}

EDIT:

So I ran the program on my PC and it is really just a ram eating program. I kept it going until windows explorer failed and my desktop went black. Then I rebooted the PC and it was fine.


Solution

  • first thing you will face with is memory leak.

    but your programme will continue to allocate memory from heap and will face with memory leak but OS will stop your programme. there is concept in OS named OOM. in this concept if some programme continue to allocate memory and cause some important part of system goes down OS will immediately send SIGKILL signal. which is cause you programme killed and then free allocated memory.