我想实现一个 C++\CLI 函数,将 System::Byte 的锯齿状数组转换为 unsigned char**。 我做了这个:
unsigned char** NUANCECLR::IsItYou::convertBBtoCC(array<array<System::Byte>^>^ b)
{
unsigned char** x = NULL;
for (size_t indx = 0; indx < b->Length; indx++)
{
if (b[indx]->Length > 1)
{
pin_ptr<System::Byte> p = &b[indx][0];
unsigned char* pby = p;
char* pch = reinterpret_cast<char*>(pby);
x[indx] = reinterpret_cast<unsigned char *>(pch);
}
else
x[indx] = nullptr;
}
return x;
}
我目前无法测试,也许有人可以帮助我,告诉我是否可以,因为我需要它比较快。谢谢!
最佳答案
不好。这将以多种不同的方式在你面前鞠躬:
unsigned char** NUANCECLR::IsItYou::convertBBtoCC(array<array<System::Byte>^>^ b)
{
unsigned char** x = NULL;
没有分配存储空间。 x[anything]
将无效。
for (size_t indx = 0; indx < b->Length; indx++)
{
if (b[indx]->Length > 1)
{
pin_ptr<System::Byte> p = &b[indx][0];
此固定指针将在此 if block 和取消固定结束时超出范围。系统可能会再次随意移动或删除
unsigned char* pby = p;
这会获取一个指向围绕一个字节的对象包装器数组的指针,并将其分配给一个 char
的数组。 .我不会在这里声称专业知识,但我不相信如果没有很多隐藏的巫术,这将无法透明地工作。
char* pch = reinterpret_cast<char*>(pby);
这实际上会起作用,但因为之前的可能不起作用,所以我不希望 pch
指向任何有意义的东西。
x[indx] = reinterpret_cast<unsigned char *>(pch);
如上所述,x
不指向任何存储。这是注定的。
}
else
x[indx] = nullptr;
也注定了
}
return x;
仍然注定失败。
}
推荐:
- 使用
new
分配非托管存储对于char *
大小数组b->Length
并分配给x
- 使用
new
分配非托管存储对于char
大小数组b[indx]->Length
并复制b
的所有元素进去,然后分配给x[indx]
. - 返回
x
- 确保
x
指向的所有数组然后x
完成后将被删除。或者使用vector<vector<char>>
而不是char**
.
关于c++ - 将 System::Byte 的交错数组转换为无符号字符**,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38926197/