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

Source: Internet
Author: User

Dynamic Allocation

In your question. You create objects in two ways. The basic difference between the two approaches is the time the object is stored.

When you run the object MyObject, this is the code. It is created as its own active variable, which means that when the object is scoped, it is also voluntarily destroyed. When you use the new object () method, the memory that the object has is allocated dynamically. This means that the object will not be destroyed until you call the Delete () method. Otherwise, it always exists. When you need to use dynamic allocation of memory to handle, you should only use the dynamic allocation method, that is, when you can use the dynamic allocation of memory when you do not use your own active variables.

Here 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. Rather than a copy of the object. Assuming you are able to accept copies or moves of objects (in most cases you should), you should use your own active storage.

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 the real problems that exist in the systems we develop.

When you really need to use dynamic allocation, you should encapsulate it in a smart pointer or other type that can have RAII characteristics (such as a standard container). Smart pointers provide the full weight semantics of objects that dynamically allocate memory. Like Std::unique_ptr and Std::shared_ptr.

Assuming you can use it properly, you basically don't need to manage your own memory (see Rule of zero in this article).

Pointers

In fact, there are many other ways to use pointers 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. Otherwise, do not use it rashly.

You need to use a reference: Sometimes the function you want to invoke needs to access your current object itself (not its copy). Then you need to pass the pointer as a parameter (no matter 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 I said earlier, suppose you can accept a copy of the object being used. Then you are not required to use references again.

Need to use polymorphic situations: typically you can only implement polymorphism by pointer or reference to an object (that is, by invoking a function based on the object's dynamic type). Assuming this is what you want, then you need to use pointers or references. Is the same, 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 such behavior, such as boost::optional (or std::optional).

When you want to reduce the compilation dependencies between files to save time: One of the great features of pointers is that you just need to declare the type of pointer that you want to use (and suppose you need to define the actual object).

This allows you to reduce the coupling between your compilation units and thus reduce compilation time.

Pimpl idiom.

When you want to invoke the interface of C or a C-style function library: In such a case. You have to use the 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 by using a smart pointer, such as invoking a member function through it.

Assuming that the called Library has applied for space for you and wants you to release it through a handle, it is undoubtedly a reasonable choice to encapsulate the handle with a smart pointer and use a custom destructor to free up memory.

When does C + + use dynamic allocation (that is, using Newkeyword)? 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.