简体   繁体   English

带数据交换的std :: vector指针

[英]std::vector pointer with data swap

In the section of code below, what would be the resultant memory structure after the swap? 在下面的代码部分中,交换后的结果内存结构是什么? Would there be a leak because they have swapped memory addresses underneath? 是否存在泄漏,因为他们已经交换了下面的内存地址? Would it be fine because they did a deep copy? 会不会因为他们做了很深的复制? what if this code was stuck inside of a class and I was swapping a working buffer with a piece of dynamic memory? 如果这段代码被卡在类中并且我用一块动态内存交换一个工作缓冲区怎么办?

#include <iostream>
#include <vector>

int main()
{
    std::vector<std::string> * ptr_str_vec =
        new std::vector<std::string>();
    ptr_str_vec->push_back("Hello");

    std::vector<std::string> str_vec;
    str_vec.push_back("World");

    ptr_str_vec->swap(str_vec);

    delete ptr_str_vec;
    //What would be the resulting structures?

    return 0;
}

EDIT: Posted slightly faulty code. 编辑:发布略有错误的代码。 Fixed the errors. 修正了错误。

When a vector is created, the underlying continuous data block used by vector is by default created from heap. 创建向量时,向量使用的基础连续数据块默认是从堆创建的。 In your case, since you didn't supply allocator, default one is used. 在您的情况下,由于您没有提供分配器,因此使用默认分配器。

int main()
{
    std::vector<std::string> * ptr_str_vec =
        new std::vector<std::string>(); // #^&! *ptr_str_vec is allocated from heap. vector's data block is allocated from heap.
    ptr_str_vec->push_back("Hello");    // #^&! "hello" is copied onto heap block #1

    std::vector<std::string> str_vec;   // #^&! str_vec is allocated from stack. vector's data block is allocated from heap.
    str_vec.push_back("World");         // #^&! "world" is copied onto heap block #2

    ptr_str_vec->swap(str_vec);         // #^&! swap is fast O(1), as it is done by swapping block #1 and #2's address. No data copy is done during swap.

    delete ptr_str_vec;                 // #^&! delete ptr_str_vec as well as heap block #2.
    //What would be the resulting structures? /

    return 0;                           // #^&! delete str_vec as well as heap block #1
}

The values in each vector will be swapped http://www.cplusplus.com/reference/vector/vector/swap/ 将交换每个向量中的值http://www.cplusplus.com/reference/vector/vector/swap/

I see no memory leak (other than the one you get when your program ends at the end of main, since you aren't deleting your pointer), your ptr_str_vec pointer does not change, only the data inside the vector that it points to changes 我看到没有内存泄漏(除了你的程序在main结束时得到的那个,因为你没有删除你的指针),你的ptr_str_vec指针不会改变,只有它指向的向量内的数据发生变化

Assuming that you are already familiar with swap , is there any reason that you haven't set it up so you can test the output to see what it does yourself? 假设您已经熟悉swap ,是否有任何理由没有设置它,以便您可以测试输出以查看它自己做什么? This will be the quickest way to assure yourself that you know exactly what it is doing and if your use of it is appropriate. 这将是确保您确切知道它正在做什么以及您是否适当使用它的最快捷方式。

In this case, the resulting structures are simply that ptr_str_vec points to a vector containing a std::string("World") and str_vec is a vector containing a std::string("Hello") . 在这种情况下,结果结构只是ptr_str_vec指向包含std::string("World")的向量, str_vec是包含std::string("Hello")的向量。 Your example suffers from many faults in answering your question, in particular because you only have a single element in each vector (and thus the vectors are equal length), and because the elements are the exact same size (and thus the vectors occupy approximately equivalent memory segments). 你的例子在回答你的问题时遇到很多错误,特别是因为你在每个向量中只有一个元素(因此向量长度相等),并且因为元素的大小完全相同(因此向量占据的大致相当)记忆段)。 In a running instance of your full project, it is very likely that none of these conditions are true. 在整个项目的运行实例中,很可能没有这些条件成立。

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

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