[英]convert jagged array of System::Byte to unsigned char**
我想實現一個C ++ \\ CLI函數,該函數將交錯的System :: Byte數組轉換為無符號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];
如果阻塞並取消固定,則此固定指針將超出范圍。 系統可能會再次隨意移動或刪除
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
分配非托管存儲,以存儲大小為b->Length
的char *
數組並分配給x
new
為大小為b[indx]->Length
的char
數組分配非托管存儲,並將b
所有元素復制到其中,然后分配給x[indx]
。 x
x
,然后x
當你與他們所做的都將被刪除。 或者使用vector<vector<char>>
代替char**
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.