C + + Dynamic Memory

Source: Internet
Author: User

1. make_shared<t> (args): Return a shared_ptr dynamically allocated object of type T. Use args to initialize the Obje Ct.

Shared_ptr<t> p (q): P is a copy of shared_ptr Q. Increase the count in Q. The pointer in Q must is convertable to T.

p = q:p and q is shared_ptr holding pointers that is convertable to one another. Decrease P ' s reference count, and increase Q ' s count; Delete P ' s existing memory if P ' s count goes to zero.

P.use_count (): Return the number of objects sharing with p. intended for debug purpose.

2. Ordinarily we use auto-make it easier to define a object to hold the result of make_shared:

Auto P1 = make_shared<revector<string>>= make_shared<int> (  // p and Q point to the same object

3. The fact that the shared_ptr class automatically free dynamic objects when they is no longer needed makes it fairly ea Sier to use dynamic memory.

 //  factory return a shared_ptr pointing to a Dynamically allocated object  Shared_ptr<foo> factory (T Arg) { //  process Arg as a appropriate  //     shared_ptr'll take care of deleting the memory  return  make_shared<foo> void   Use_factory (T Arg) {shared_ptr  <Foo> p = //  use P } //  p goes out of scope. The memory to which P points are automatically free  

4.If you put shared_ptrs into a container, you should is sure to erase shared_ptr elements once you no longer need those E Lements.

Programs tend to use the dynamic memory for one of three purpose:

    • They don ' t know how many object they would need
    • They don ' t know the precise type of the object they need.
    • They want to share data between Serval objects.

So far, the classes we have used allocate resources, which exist only as long as the corresponding object

vector<string> v1;{    Vector<string> v2 = {"a""aa""  BBB"};     = v2;    // copies the elements in v2 to v1}    // v2 is deleted, which destroys the elements in v2      // V1 has three new copied elements

Operators allocate and delete dynamic memory:

    • New:allocates Memory
    • Delete:frees memory allocated by new.

Use these-operator is more error-prone than using a smart pointer.

A dynamic object managed through a build-in pointer exists until it is explictly deleted

Foo Factory (T Arg) {    returnnew Foo (ARG);    // Caller is responsible for deleting this memory }void  use_factory (T Arg) {    *p = use_factory (ARG);     // Use p but does not delete it  out  is not freeed.

In this example, p is the only pointer to memory allocated by factory. Once Use_factory Returns, the program has no-to-free the memory. Then memory leak.

There is three common problem with using new and delete to manage dynamic memory:

    • Forgetting to delete memory, which is known as memory leak
    • Using A object after it has been deleted
    • Deleting the same object twice

We should use smart pointers rather than plain pointers

If We do not initialize a smart pointer, it is initialized as a null pointer. We can also initialize a smart pointer from a pointer return from new

shared_ptr<Double> p1;shared_ptr<int> P2 (newint(42 ));

The smart pointer constructors that take pointers is explict. We can not be implictly conver a build-in pointer to a smart pointer.

shared_ptr<intnewint(1024x768);    // errorshared_ptr<int> P2 (newint(1024x768));    // OK. Use Direct initilization

A function, return a shared_ptr cannot implictly return a Plian pointer in its return statement

shared_ptr<int> Clone (int  p) {    returnnewint(p);    // Error }shared_ptr<int> Clone (int  p) {    //  OK; Explicitly create a shared_ptr from int *    return shared_ptr<int> (New  int(P));}

Don ' t mix ordinary pointers and smart pointers.

When we bind a shared_ptr to a pain pointer, we give responsibility for this memory to the shared_ptr, and we should no lo Nger use a build-in pointer to access the memory of which the shared_ptr now points.

Don ' t use the get to Initilize or assign another smart pointer.

C + + Dynamic Memory

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.