简体   繁体   English

从构造函数中本地声明的device_vector使用device_vector :: data()方法初始化结构内部的指针是否安全?

[英]It is safe to initialize a pointer inside a struct using device_vector::data() method from a locally declared device_vector in the constructor?

I have coded some functors that initialize a member pointer in this way: 我已经编写了一些函子,以这种方式初始化成员指针:

struct A
{
    A() {
        thust::device_vector<int> d_vect(3);
        d_vect[0]=1;
        d_vect[1]=2;
        d_vect[2]=3;

        pointer = thrust::raw_pointer_cast(d_vect.data());
    }

    __host__ __device__ void operator() {
        //code using pointer
    };

    int* pointer;

};

The functor coded in this way run without any apparent problem, so I wounder what happens to the data stored in d_vect . 以这种方式编码的函子可以运行,没有任何明显的问题,因此我很担心d_vect存储的数据会发生什么。 Reading the documentation I have the impression this data should be destroyed when the d_vect goes out of the scope, so I would expect the data point by pointer should get erased, but it is not. 阅读文档时,我认为当d_vect超出范围时,该数据应被销毁,因此我希望逐个pointer的数据应被擦除,但事实并非如此。 Please could some one clarify this thing to me? 请有人可以向我澄清一下吗?

Your understanding is correct: thrust::device_vector is a managed container and frees all allocated storage when it goes out of scope. 您的理解是正确的: thrust::device_vector是一个托管容器,当超出范围时将释放所有分配的存储。

Now de-allocation merely updates bookkeeping information about which memory blocks are free to use, it doesn't explicitly overwrite those blocks with say zeros (for performance reasons). 现在,解除分配只是更新有关哪些存储块可以免费使用的簿记信息,它不会用零来明确覆盖那些块(出于性能原因)。 You have to do that manually if that's the behavior you wish. 如果这是您希望的行为,则必须手动执行。

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

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