c++ - 如何设计可反弹的大对象的类依赖?

标签 c++ pointers reference smart-pointers

我需要更改内部变量以引用新对象,然后做一些事情,然后再次更改对象,然后对新值做一些事情。

class dml
{
     void setTrain(matrix<T> train_x , matrix<T> train_y)
     {
          train_x_ = train_x;
          train_y_ = train_y;
     }
     void train();

     private:
          matrix<T> train_x_;
          matrix<T> train_y_;
}

因此,我将首先设置 train ,然后调用 train ,它使用我刚刚设置的值进行训练。然后稍后我可能会通过对新值调用 setTrain 来更改值,我想再次对该数据运行训练。 (对于 ML 的人,我想实现小批量,我输入数据,训练它,然后输入下一批数据,然后训练它) 我现在拥有的简单解决方案很愚蠢,因为它涉及复制一个非常大的矩阵。我的指针是想到的简单解决方案,但我不想使用它们。我的程序很复杂,指针和内存分配只会更麻烦。 我想到的一个想法是:

     private:
          matrix<T>& train_x_;
          matrix<T>& train_y_;
}

当然这是行不通的,因为引用永远不能指向空值,我必须在构造函数中初始化它们,因为它们不能重新绑定(bind),所以没有必要使用它们。

我认为解决方案是使用一些智能指针。但是智能指针用于管理堆中的内存,但我不是在堆中分配内存。矩阵对象是在堆栈中创建的,但它是使用 std::vector 实现的,它初始化堆中的所有对象。

我认为逻辑上 shared_ptr 应该起作用,因为我们正在共享一个变量。 std::make_shared 似乎是个好主意,但我读到,当您尝试从堆栈中的现有对象创建 shared_ptr 时,它也会创建一个拷贝。但这并没有解决必须复制这么大对象的问题。

另一个明显的事情是 std::move,但是 move 在这里不适用,因为我可能需要在函数调用之外使用训练数据 agian。

我确定我遗漏了一些明显的东西。我现在有点 sleep 不足,所以任何解决方案都会对我有很大帮助!

澄清一下:

LargeObj a=9;
auto a_ptr=std::make_shared(a);

如果我这样做,那么 a_ptr 将只指向 a,而不创建拷贝?但这不是这里的一个答案所说的:

Create a boost::shared_ptr to an existing variable

int a = 3;//现有变量 shared_ptr aCopy = make_shared(a);//创建值为a的拷贝

最佳答案

LargeObj a=9;
auto a_ptr=std::make_shared(a);

If I do this, then a_ptr will just point to a, without creating a copy?

如果您通过复制现有对象(允许)来创建共享指针,那么您将拥有两个对象。这可以通过删除类中的相关函数和/或永远不要将对象定义为 shared_ptr<matrix<T>> 以外的任何对象来避免。 .


如果使用 std::shared_ptr ,您可以处理大对象并防止复制.

以下是对问题中示例的改编。它将对象创建为 std::shared_ptr s 并显示如何将它们分配给示例中的类。

#include <memory>
#include <iostream>
#include <string>

template <typename T>
class matrix {
public:
    matrix() = default;
    matrix(const matrix&) = delete; // no copy
    matrix(matrix&&) = default; // allow move
    matrix& operator=(const matrix&) = delete; // no copy
    matrix& operator=(matrix&&) = default; // allow move
};

template <typename T>
class dml
{
public:
    void setTrain(std::shared_ptr<matrix<T>> train_x_ptr, std::shared_ptr<matrix<T>> train_y_ptr)
    {
        train_x_ = train_x_ptr;
        train_y_ = train_y_ptr;
    }
    void train() {};
private:
    std::shared_ptr<matrix<T>> train_x_{}; // starts out as an empty shared pointer (contains nullptr)
    std::shared_ptr<matrix<T>> train_y_{}; // starts out as an empty shared pointer (contains nullptr)
};

int main()
{
    // no explicit new, object is created on the heap (train_x_ptr is created on the stack,
    // but will be copied to dml without copying the underlying object).
    auto train_x_ptr{std::make_shared<matrix<int>>()}; 
    auto train_y_ptr{std::make_shared<matrix<int>>()};
    dml<int> d;
    d.setTrain(train_x_ptr, train_y_ptr);
}

请注意 new不直接使用。

I think the solution would be in using some of the smart pointers. But smart pointers are used to manage the memory in heap, but I am not allocating an memory in the heap. The matrix object is created in the stack, but it is implemented using a std::vector, which initializes all of its objects in the heap.

在此示例中,std::shared_ptr将创建 matrix在堆上并为您管理生命周期。同时,如果matrix包含 std::vector这些元素也将在某个地方的堆上,由 std::vector 管理.

I think logically shared_ptr should work, as we are sharing a varaible. std::make_shared seems to be a good idea, but I read that it too creates a copy when you try create a shared_ptr from an existing object in the stack. But this does not solve the problem of having to copy such a large object.

它不会创建拷贝。它确实解决了问题。

关于c++ - 如何设计可反弹的大对象的类依赖?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41125733/

相关文章:

c++ - 函数模板取函数模板

c++ - CInternetSession 安全吗?它被加密了吗?

c++ - 我如何在 C++ 中将一个类的指针移动到另一个类

c++ - 在指针上执行运算符而不取消引用

python - 函数是否有一种通用的方法来引用自身?

c++ - 当我尝试输入值时程序崩溃

c++ - SHGetKnownFolderItem - 在 Wow64 上失败

c++ - 从 dll/共享库返回指针会导致段错误

C 在 void 函数之后由指针发送的数组的值被修改

rust - 如何在循环中更新可变引用?