1

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;
}
10
  • 4
    I can't make any sense out of your question but I think you need to spend some time studying the basics of C++. Commented Sep 19, 2016 at 13:59
  • you dont need to manually free the memory of unique and shared ptr. They will do it themself. And you always derefernce your unique ptr array. You can just keep it on the stack that way instead of instantiating it on the heap. Commented Sep 19, 2016 at 14:00
  • A friend of mine who has worked as a developer for some time once told me that it's better to explicitly free memory when you know you don't need it and then when you forget then the smart_ptr funcionality can act as a safety net. Is that him just being pedantic or are there times when relying on smart_ptr's to free memory can cause memroy leak? Commented Sep 19, 2016 at 14:06
  • 4
    @Flash_Steel that's just being pedantic. The whole concept behind this RAII view is so that you can forget about them and concentrate on what is important. Commented Sep 19, 2016 at 14:08
  • 2
    Rather than release early I think it would be better to limit the scope of the smart pointers. Otherwise you can end up dereferencing null smart pointers or having to always check if they are not null. My suggestion would be, as far as possible, create them as not-null and don't null them before their scope ends. Commented Sep 19, 2016 at 14:30

2 Answers 2

2

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Less bloated could well be MyClass({{1., 2.}});. Difficult to say because most of OP's code seems to have no reason.
well I assumed the shared_ptr necessary and that you put an array in there. But year for OP code this would be the least code.
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.