简体   繁体   English

使用shared_ptr返回向量的指针

[英]return a pointer of vector with shared_ptr

I am testing with the shared_ptr of vector. 我正在测试vector的shared_ptr。 The purpose is simple, I want to return a pointer of vector and access the value in it. 目的很简单,我想返回vector的指针并访问其中的值。 But it gives exception. 但是它给出了例外。 "Unhandled exception at.. std:out_of_range at memory location ..". “在内存位置.. std:out_of_range处未处理的异常。”。 I am using Visual Studio 2012. 我正在使用Visual Studio 2012。

vector<int>* func()
{
    boost::shared_ptr<vector<int> > vec(new vector<int>());

    vec->push_back(123);
    return vec.get();
    }

int _tmain(int argc, _TCHAR* argv[])
{
    vector<int>* result = func();

    cout << result->at(0); // ERROR here
    return 0;
}

If you want to use shared pointers, then return a shared pointer, not a raw one. 如果要使用共享指针,则返回一个共享指针,而不是原始指针。 Otherwise, when the shared_ptr goes out of scope, it will destroy the vector. 否则,当shared_ptr超出范围时,它将破坏向量。 Accessing that vector will lead to undefined behaviour. 访问该向量将导致不确定的行为。

boost::shared_ptr<vector<int> > func()
{
    boost::shared_ptr<vector<int> > vec(new vector<int>());
    vec->push_back(123);
    return vec;
}

However, note that it's much better to return the vector by value: 但是,请注意,最好通过值返回向量:

vector<int> func()
{
    vector<int> vec;
    vec.push_back(123);
    return vec;
}

This way, there are no copies being made, either through move constructors or RVO . 这样,就不会通过move构造函数或RVO进行任何复制。

The type you return from func() needs to be boost::shared_ptr<vector<int>> - not vector<int>* . func()返回的类型需要为boost::shared_ptr<vector<int>> -而不是vector<int>*

The whole point of shared pointers is you can pass them around as much as you like and when they all stop being referenced the memory they pointed to is reclaimed. 共享指针的全部目的是您可以随意传递它们,并且当它们全部停止被引用时,它们所指向的内存将被回收。

As you "forget" the reference to the shared pointer when you leave the function it will automatically reclaim the memory assigned at that point - leaving you with a pointer to an invalid memory location. 离开函数时,当您“忘记”对共享指针的引用时,它将自动回收在该点分配的内存-留下指向无效内存位置的指针。

Your shared_ptr in your function is the only one referencing the pointer to your vector . 函数中的shared_ptr是唯一一个指向vector指针的指针。 When it goes out of scope (when the function returns), it therefore deletes the referenced pointer. 当它超出范围时(函数返回时),因此将删除引用的指针。

Make your function return a shared_ptr instead of a regular pointer. 使您的函数返回shared_ptr而不是常规指针。

I'd suggest you read up on how shared_ptr should be used, cause you're doing it all wrong.. It's meant to not having to deal with raw pointers anymore, definitely not mixing them. 我建议您阅读应如何使用shared_ptr,因为您做错了所有。.这意味着不必再处理原始指针,绝对不要混合使用它们。 Basically you pass around the shared_ptr instance like you used to with raw pointers, but without having to care about deleting it anymore. 基本上,您像以前使用原始指针那样传递了shared_ptr实例,但是不必再担心将其删除。 Do this instead: 改为这样做:

typedef std::vector< int > my_vector;
typedef boost::shared_ptr< my_vector > my_vector_ptr;

my_vector_ptr func()
{
  my_vector_ptr vec( boost::make_shared< my_vector >() );
  vec->push_back(123);
  return vec.get();
}

int _tmain(int argc, _TCHAR* argv[])
{
  my_vector_ptr result = func();
  cout << result->at(0);
  return 0;
}

Use back inserter to avoid mixing inserting and returning the same vector. 使用后置插入器可避免混合插入和返回相同的向量。 Let the client specify witch type of vector it is ... thus making the function template-able. 让客户端指定向量的类型...从而使函数可模板化。

code: 码:

typedef std::vector< int > my_vector;
typedef boost::shared_ptr< my_vector > my_vector_ptr;

template <typename OutputIterator>
void func1(OutputIterator it)
{
   // std::copy (anotherVector.begin(), anotherVector.end(), it);
   *it++ = 123; 
}

void func2(my_vector& v)
{
   v.push_back(123);
}

int main()
{
    my_vector_ptr vec( new my_vector() );
    func1(std::back_inserter(*vec)); // func is now an algorithm .. 
    func2(*vec);
}

In func1 , the signature of the function says what this function does. func1 ,函数的签名说明了此函数的作用。 You can't erase from vector, you can't do anything else, just what it says. 您不能从向量中擦除,也不能做任何其他事情,正如它所说的那样。 Insert. 插入。

Think of func1 as an algorithm. func1视为一种算法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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