gpt4 book ai didi

c++ - 将 System::Byte 的交错数组转换为无符号字符**

转载 作者:行者123 更新时间:2023-11-28 05:33:16 24 4
gpt4 key购买 nike

我想实现一个 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;

仍然注定失败。

}

推荐:

  1. 使用 new 分配非托管存储对于 char *大小数组 b->Length并分配给 x
  2. 使用 new 分配非托管存储对于 char大小数组 b[indx]->Length并复制 b 的所有元素进去,然后分配给x[indx] .
  3. 返回x
  4. 确保x指向的所有数组然后 x完成后将被删除。或者使用 vector<vector<char>>而不是 char** .

关于c++ - 将 System::Byte 的交错数组转换为无符号字符**,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38926197/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com