Why use new and delete at all?

后端 未结 8 1973
闹比i
闹比i 2020-12-06 19:25

I\'m new to C++ and I\'m wondering why I should even bother using new and delete? It can cause problems (memory leaks) and I don\'t get why I shouldn\'t just initialize a va

8条回答
  •  隐瞒了意图╮
    2020-12-06 19:46

    When you are using New then your object stores in Heap, and it remains there until you don't manually delete it. but in the case without using new your object goes in Stack and it destroys automatically when it goes out of scope. Stack is set to a fix size, so if there is no any block for assign a new object then Stack Overflow occurs. This often happens when a lot of nested functions are being called, or if there is an infinite recursive call. If the current size of the heap is too small to accommodate new memory, then more memory can be added to the heap by the operating system.

提交回复
热议问题