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

前端 未结 11 826
甜味超标
甜味超标 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:04

    The short answer is: if you're a beginner in C++, you should never be using new or delete yourself.

    Instead, you should use smart pointers such as std::unique_ptr and std::make_unique (or less often, std::shared_ptr and std::make_shared). That way, you don't have to worry nearly as much about memory leaks. And even if you're more advanced, best practice would usually be to encapsulate the custom way you're using new and delete into a small class (such as a custom smart pointer) that is dedicated just to object lifecycle issues.

    Of course, behind the scenes, these smart pointers are still performing dynamic allocation and deallocation, so code using them would still have the associated runtime overhead. Other answers here have covered these issues, and how to make design decisions on when to use smart pointers versus just creating objects on the stack or incorporating them as direct members of an object, well enough that I won't repeat them. But my executive summary would be: don't use smart pointers or dynamic allocation until something forces you to.

    0 讨论(0)
  • 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
    0 讨论(0)
  • 2020-11-22 06:11

    Which method should I use?

    This is almost never determined by your typing preferences but by the context. If you need to keep the object across a few stacks or if it's too heavy for the stack you allocate it on the free store. Also, since you are allocating an object, you are also responsible for releasing the memory. Lookup the delete operator.

    To ease the burden of using free-store management people have invented stuff like auto_ptr and unique_ptr. I strongly recommend you take a look at these. They might even be of help to your typing issues ;-)

    0 讨论(0)
  • 2020-11-22 06:13

    The short answer is yes the "new" keyword is incredibly important as when you use it the object data is stored on the heap as opposed to the stack, which is most important!

    0 讨论(0)
  • 2020-11-22 06:14

    Are you passing myClass out of a function, or expecting it to exist outside that function? As some others said, it is all about scope when you aren't allocating on the heap. When you leave the function, it goes away (eventually). One of the classic mistakes made by beginners is the attempt to create a local object of some class in a function and return it without allocating it on the heap. I can remember debugging this kind of thing back in my earlier days doing c++.

    0 讨论(0)
  • 2020-11-22 06:14

    The second method creates the instance on the stack, along with such things as something declared int and the list of parameters that are passed into the function.

    The first method makes room for a pointer on the stack, which you've set to the location in memory where a new MyClass has been allocated on the heap - or free store.

    The first method also requires that you delete what you create with new, whereas in the second method, the class is automatically destructed and freed when it falls out of scope (the next closing brace, usually).

    0 讨论(0)
提交回复
热议问题