固定空数组
在 C++/CLI 中,是否可以固定不包含元素的数组?
例如,
array<System::Byte>^ bytes = gcnew array<System::Byte>(0);
pin_ptr<System::Byte> pin = &bytes[0]; //<-- IndexOutOfRangeException occurs here
MSDN 给出的建议不包括空数组的情况。 http://msdn.microsoft.com/en -us/library/18132394%28v=VS.100%29.aspx
顺便说一句,您可能想知道为什么我想固定一个空数组。简短的答案是,为了代码简单性,我想将空数组和非空数组视为相同。
In C++/CLI, is it possible to pin an array that contains no elements?
e.g.
array<System::Byte>^ bytes = gcnew array<System::Byte>(0);
pin_ptr<System::Byte> pin = &bytes[0]; //<-- IndexOutOfRangeException occurs here
The advice given by MSDN does not cover the case of empty arrays.
http://msdn.microsoft.com/en-us/library/18132394%28v=VS.100%29.aspx
As an aside, you may wonder why I would want to pin an empty array. The short answer is that I want to treat empty and non-empty arrays the same for code simplicity.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,不能使用 pin_ptr<>。您可以回退到 GCHandle 来实现相同的目的:
在我看来,您应该使用
List^
来代替。Nope, not with pin_ptr<>. You could fallback to GCHandle to achieve the same:
Sounds to me you should be using
List<Byte>^
instead btw.您无法固定具有 0 个零元素的 cli 对象
array
,因为该数组没有内存支持。显然,您无法固定没有内存可指向的东西。然而,cli 对象
array
元数据仍然存在,并且它声明数组长度为 0。You cannot pin a cli object
array
with 0 zero elements because the array has no memory backing. You obviously cannot pin something that has no memory to point to.The cli object
array
metadata still exists, however, and it states that the array length is 0.