2016-08-12 91 views
0

我想實現將System :: Byte的鋸齒陣列轉換爲unsigned char **的C++ \ CLI函數。 我做了這個:將System :: Byte的鋸齒陣列轉換爲無符號字符**

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; 
} 

我目前無法測試它,也許有人可以幫助我,告訴我,如果它是確定或沒有,因爲我需要它比較fast.Thank你!

回答

1

不好。這將在您的臉上以多種方式鞠躬:

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]; 

這個pING指針將超出範圍在這個if塊的結尾並取消固定。該系統可以再次移動或隨意

   unsigned char* pby = p; 

這需要一個指向圍繞一個字節對象wappers的陣列,併爲其分配到刪除的char陣列。我不會在這裏要求專業知識,但我不相信這將透明地工作,沒有很多隱藏的巫術。

   char* pch = reinterpret_cast<char*>(pby); 

這將實際工作,但becasue以前可能沒有,我不希望在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**
+0

謝謝,你是對的,我忘了分配,我的大腦是KO :) – WhiteR0205