When does C + + use dynamic allocation (that is, using the new keyword)? When to use pointers?

Source: Internet
Author: User

Dynamic Allocation

In your problem, you create objects in two ways. The main difference between the two approaches is the storage time of the object. When executing object MyObject, this code is created as an automatic variable, which means that the object is automatically destroyed when it is scoped. When you use new object () This way, the memory that the object has is allocated dynamically, which means that the object is not destroyed until you call the Delete () method, otherwise it persists. When you need to allocate memory dynamically, you should only use the dynamic allocation method, that is, when you can use the dynamic allocation of memory when you do not use automatic variables.

The following are two common scenarios in which you might use dynamic allocations:

1. When you want an object to exist after it has been scoped-and it is indeed the object that was previously stored in that memory, not a copy of the object. If you can accept the use of the object copy or move (in most cases you should), then you should use the automatic storage method.

2. When a large amount of memory is required, it is extremely easy to cause stack overflow in this case. Of course it would be better if it wasn't a problem for you (in most cases it's impossible). This is clearly beyond the jurisdiction of C + +, but unfortunately we have to deal with this real-world problem in the systems we develop.

When you do need to use dynamic allocation, you should encapsulate it in a smart pointer or other type that can have RAII attributes (such as a standard container). Smart pointers provide ownership semantics for objects that dynamically allocate memory. such as Std::unique_ptr and Std::shared_ptr. If you can use it properly, you basically don't need to manage the memory yourself (see Rule of zero in this article).

Pointers

In fact, pointers have many other uses in addition to dynamically allocating memory, but most of them have better choices than they do. As I said before, unless you have to use pointers, don't use them rashly.

You need to use a reference: Sometimes the function you want to call needs to access your current object itself (not its copy), then you need to use the pointer as a parameter to pass it (regardless of how it is allocated). However, in most cases, using references is better than pointers, which is why references are designed. Note that there is no need to extend the life cycle of an object as described above. As already mentioned, if you can accept a copy of the object, then you don't need to use the reference again.

Polymorphic situations are required: typically you can only implement polymorphism through pointers or references to objects (that is, calling functions based on the object's dynamic type). If this is what you want, then you need to use pointers or references. Again, the pointer is the preferred choice.

When an object is ignorable, implementing an object by passing a null pointer is an optional property: If it is a parameter, you should take precedence over the default parameter or the method of function overloading. Otherwise you should choose a type that can encapsulate this behavior, such as boost::optional (or std::optional).

When you want to reduce compilation dependencies between files to save time: One of the great features of pointers is that you just need to declare the type that the pointer is pointing to (and if you want to use the actual object, you need to define it). This allows you to reduce the coupling between your compilation units and thus reduce compilation time. Refer to Pimpl idiom.

When you want to invoke the interface of C or a C-style function library: In this case, you have to use a pointer to manipulate it. The only thing you can do is to make sure that your pointer is released when you're not using it. You can also manipulate the original pointer through a smart pointer, for example, by invoking a member function. If the called Library has requested space for you and wants you to release it through a handle, it is a reasonable choice to encapsulate the handle with a smart pointer and use a custom destructor to free up memory.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

When does C + + use dynamic allocation (that is, using the new keyword)? When to use pointers?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.