When a class dynamically allocates itself at constructor, why does stack overflow happen instead of std::bad_alloc?
The stack overflow is happening because you have infinite recursion. Calling Overflow()
causes you to call Overflow()
again and again and again. Those function calls need to go on the stack. Since your stack is smaller than your heap you'll run out of stack space for all of the those constructor calls before you run out of memory for all of the objects you are creating.
Because you are recursively calling the constructor, a method, repeatedly. The method calls get pushed to the call stack. Since the stack size is much smaller than the available heap, the call stack overflows before the heap runs out.