2013-11-14 100 views
1

我需要從非託管C++庫包串向量,這樣做我有一個vector<string>轉換爲char***。對於此轉換,我正在使用一個函數。但是,即使它看起來沒錯,一旦出現指針值變爲無效。C++:指針值改退出功能

這裏是它的一個簡單的例子:

int main() { 
    cout << "Start of the program" << endl; 

    vector<string> vStr = vector<string>(); 
    vStr.push_back("ABC"); 
    vStr.push_back("DEF"); 
    vStr.push_back("GHI"); 
    vStr.push_back("JKL"); 
    vStr.push_back("MNO"); 
    vStr.push_back("PQR"); 

    char*** pStr = new char**(); 
    int* pLength = new int(); 

    cout << " > before export" << endl; 

    exportVector(vStr, pStr, pLength); 

    cout << " < after export" << endl; 

    cout << "\t pLength value = " << *pLength << endl; 

    for (unsigned int i = 0 ; i < vStr.size(); i++) 
    { 
     cout <<"\t pStr "<< i << ": " << vStr[i] << " to "; 
     for(unsigned int j = 0; j < 3; ++j) 
     { 
      cout << "-" << pStr[0][i][j]; 
     } 
     cout << "-"<< endl; 
    } 

    cout << "End of the program" << endl; 

    delete pStr; 
    delete pLength; 

    return 0; 
} 

void exportVector(vector<string> vect, char*** pData, int* pSize) 
{ 
    vector<char*> charVect = vector<char*>(vect.size()); 
    //cout << "\t charVect.size() = " << charVect.size() << endl; 

    // Copy and cast elements of given vector into chars 
    for(unsigned int i = 0; i < vect.size() ; i++) 
    { 
     charVect[i] = const_cast<char*>(vect[i].c_str()); 
    } 

    *pData = &charVect[0]; 
    *pSize = vect.size(); 

    cout << "\t pSize = " << *pSize << endl; 
    for (unsigned int i = 0 ; i < vect.size(); i++) 
    { 
     cout <<"\t pData "<< i << ": "; 
     for(unsigned int j = 0 ; j < 3 ; ++j) 
     { 
      cout << "-" << pData[0][i][j]; 
     } 
     cout << "-"<< endl; 
    } 
} 

而且我越來越控制檯:

Start of the program 
> before export 
    pSize = 6 
    pData 0: -A-B-C- 
    pData 1: -D-E-F- 
    pData 2: -G-H-I- 
    pData 3: -J-K-L- 
    pData 4: -M-N-O- 
    pData 5: -P-Q-R- 
< after export 
    pLength value = 6 
    pStr 0: ABC to -Ä- -i- 
    pStr 1: DEF to - - -i- 
    pStr 2: GHI to -G-H-I- 
    pStr 3: JKL to -J-K-L- 
    pStr 4: MNO to -M-N-O- 
    pStr 5: PQR to -P-Q-R- 
End of the program 

如何解釋裏面exportVector功能外它值之差?如何糾正?

非常感謝。

+3

僅僅因爲你需要使用那些醜陋的三重指針,那沒有理由讓其餘的代碼不好。繼續使用'vector',放棄'new'廢話。 –

+0

要指出的一件事情很多:你一定要明白,你可以只聲明'int'更換'pLength'和地址'exportVector'通過它,對嗎? – defube

+0

感謝您的回答。不幸的是,正如我所說的,我需要這個三重指針來進行編組......是的,我知道我可以用另一種方式來處理pLength。 – cfrog

回答

1

您需要通過參考

void exportVector(vector<string> &vect, char*** pData, int* pSize) 

通過你的載體,你也應該的strdup在這裏使用:

charVect[i] = const_cast<char*>(vect[i].c_str()); 
//should be 
charVect[i] = strdup(vect[i].c_str()); 

最後,

vector<char*> charVect = vector<char*>(vect.size()); 
*pData = &charVect[0]; 

將段錯誤原因charVect將被銷燬在函數結尾處, 您需要刪除這些行並執行一些操作像這樣:

(*pData) = new char*[vect.size()]; 
// Copy and cast elements of given vector into chars 
for(unsigned int i = 0; i < vect.size() ; i++) 
{ 
    (*pData)[i] = strdup(vect[i].c_str()); 
} 
+0

謝謝你。事實上,我可以用另一種方式來解決問題。但是它並沒有解釋pStr的問題。 – cfrog

+0

您需要通過引用傳遞vect,因爲如果不是它被複制到堆棧中,並且在exportVector函數結束時它將被其數據銷燬 – izissise

+0

不應該保持一致嗎?在這種情況下,對vect的引用將無濟於事...... – cfrog

1

您是按值傳遞載體的exportVector功能!該函數返回後矢量被破壞,並且你的char *指針變得孤立,指向內存位置,這些內存位置可能在之後被用於其他目的...

另外,你的const_cast應該已經響了很多警鐘!千萬不要做一個const_cast(除了極少數情況下,你知道它是安全的,這樣做的 - 這是不是其中之一)!

但這些東西是不是在你的代碼的唯一問題。你認爲char*** pStr = new char**();在做什麼? 它的作用是,它保留一個指針的指針空間爲char(通常4-8個字節)。你似乎認爲神奇地爲大量字符數組分配空間?

+0

好的。但是,我們如何才能使其一致? – cfrog