When should I use the new keyword in C++?

前端 未结 11 1068
甜味超标
甜味超标 2020-11-22 05:47

I\'ve been using C++ for a short while, and I\'ve been wondering about the new keyword. Simply, should I be using it, or not?

1) With the new keywo

11条回答
  •  面向向阳花
    2020-11-22 06:08

    Method 1 (using new)

    • Allocates memory for the object on the free store (This is frequently the same thing as the heap)
    • Requires you to explicitly delete your object later. (If you don't delete it, you could create a memory leak)
    • Memory stays allocated until you delete it. (i.e. you could return an object that you created using new)
    • The example in the question will leak memory unless the pointer is deleted; and it should always be deleted, regardless of which control path is taken, or if exceptions are thrown.

    Method 2 (not using new)

    • Allocates memory for the object on the stack (where all local variables go) There is generally less memory available for the stack; if you allocate too many objects, you risk stack overflow.
    • You won't need to delete it later.
    • Memory is no longer allocated when it goes out of scope. (i.e. you shouldn't return a pointer to an object on the stack)

    As far as which one to use; you choose the method that works best for you, given the above constraints.

    Some easy cases:

    • If you don't want to worry about calling delete, (and the potential to cause memory leaks) you shouldn't use new.
    • If you'd like to return a pointer to your object from a function, you must use new

提交回复
热议问题