固定矢量 [英] Pinning Vectors

查看:96
本文介绍了固定矢量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的情况是我有一个矢量< gcroot< ManagedWrapperClass *> >

其内容我需要传递给非托管函数。是否有一种方法

来固定该函数向量中的所有指针?我试图将
复制到带有固定指针类型的另一个向量中,但

编译器不允许在模板参数中使用__pin。


谢谢!


-

Brian

I have a situation where I have a vector<gcroot<ManagedWrapperClass*> >
whose contents I need to pass to an unmanaged function. Is there a way
to pin all the pointers in the vector for that function? I tried to
copy the vector into another vector with a pinned pointer type, but the
compiler will not allow __pin to be used in a template parameter.

Thanks!

--
Brian

推荐答案

__ gc class X

{


};


__nogc class W

{

vector< gcroot< X *> > arr;

public:

W()

{

X __pin * p = new X;

arr.push_back(p);

}

};


这样可以吗?


Ismail

__gc class X
{

};

__nogc class W
{
vector<gcroot<X*> > arr;
public:
W()
{
X __pin* p = new X;
arr.push_back(p);
}
};

is this OK?

Ismail


2005年5月26日晚上9点37分,ismailp写道:
At 9:37 PM on May 26, 2005, ismailp wrote:
X __pin * p = new X;
arr.push_back(p);
[snip]这样可以吗?
X __pin* p = new X;
arr.push_back(p); [snip]is this OK?




我不确定是不是。我对固定指针的理解是

__pin关键字适用于本地范围内的变量,当该变量超出范围时,引脚是释放的.b $ b。所以在我看来,上面代码中的
push_back将一个指针隐式地转换为非固定指针,然后固定指针离开

范围,释放销钉。


如果我的理解不合适,我会很高兴纠正。


-

Brian



I''m not sure it is. My understanding of pinned pointers is that the
__pin keyword applies to the variable in the local scope and the pin is
released when that variable goes out of scope. So it seems to me that
push_back in the above code pushes a pointer that is implicitly
converted to a non-pinned pointer, then the pinned pointer goes out of
scope, releasing the pin.

If my understanding is off, I would be happily corrected.

--
Brian


ismailp写道:
ismailp wrote:
__gc class X
{

};

__nogc类W
{
向量< gcroot< X *> > arr;
公开:
W()
{x / _pin * p =新X;
arr.push_back(p);
}
Ismail
__gc class X
{

};

__nogc class W
{
vector<gcroot<X*> > arr;
public:
W()
{
X __pin* p = new X;
arr.push_back(p);
}
};

is this OK?

Ismail




不,不是。引脚立即释放。


我认为你必须在后台自动复制__pin正在做的系统调用

。我不知道它是哪一个。在其他

字样中,您需要一个不受范围限制的钉扎机制。


Tom



No it''s not. The pin is released right away.

I think you have to duplicate the system call that __pin is doing
automatically in the background. I don''t know which one it is. In other
words, you need a pinning mechanism that''s not scope limited.

Tom


这篇关于固定矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆