简体   繁体   中英

Std::Array with unique_ptr

I have an class which I wish to instantiate by passing an array of values. The object here has two members but this has been reduced for illustration. In the future I will read values from disk and then create an object from those values, hence the array. The object will have multiple pointers later on hence the shared_ptr.

Firstly, I would like to know if this would prevent memory leaks as is. Secondly, I would like to know if there is are less bloated ways of instantiating objects and then determinsitically destroying them later on.

Class Header file:

//MyClass.hpp
#pragma once
#include "stdafx.h"
#include <array>

class SimpleClass{
    private:
        //Per object
        double var1;
        double var2;
    public static:
        //For calling when using templates such as std::array<>
        //Using __int8 as will not be > 256
        static const uint8_t varCount 2;
        SimpleBody(std::array<double, varCount> inputArray);
        ~SimpleBody();
}

Class Implementation

//MyClass.cpp
#pragma once
#include "stdafx.h"
#include <array>
#include "body.hpp"

SimpleBody::SimpleBody(std::array<double, SimpleBody::varCount> inputArray ) {
    //Assign var1
    MyClass::var1= inputArray[0];
    //Assign var2
    MyClass::var2= inputArray[1];
};

SimpleBody::~SimpleBody() {
    //Add in code here when children need deleting
}; 

Entry Point

// EntryPoint.cpp
//

#include "stdafx.h"
#include "MyClass.hpp"
#include <array>
#include <memory>

int main()
{
    //Create an array with a smart pointer for memory management
    std::unique_ptr< std::array<double, MyClass::varCount> > initArray =
        std::make_unique< std::array<double, MyClass::varCount> >();
    //Define values
    *initArray = { 1.0,2.0 };
    //Use array to create object
    std::shared_ptr<MyClass> object = std::make_shared<MyClass>(*initArray );

    //Free memory
    initArray.reset();
    object.reset();

    return 0;
}

First of all its bad if you pass the array by value in the SimpleBody constructor. Better use a reference

 SimpleBody(const std::array<int, varCount> &inputArray);

At the moment, you construct your shared_ptr the unique_ptr looses the ownership of the array. You don't need to do reset manually. Also, at the moment, your program hits the } line, the memory will be freed.

In your example you could skip the unique pointer because you only want the values anyway

int main()
{
    std::array<double, MyClass::varCount> initArray = { 1.0,2.0 };
    std::shared_ptr<MyClass> object = std::make_shared<MyClass>(initArray );
    return 0;
}

This would do the same.

For "less bloated":

There is always auto :

int main()
{
    auto initArray = std::make_unique< std::array<double,  MyClass::varCount> >();
    *initArray = {1.0,2.0};
    auto object = std::make_shared<MyClass>(*initArray );
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM