簡體   English   中英

C ++:指針值已更改,退出函數

[英]C++: Pointer value changed exiting a function

我需要包裝來自非托管C ++庫的字符串向量,並且這樣做必須將vector<string>轉換為char*** 對於此轉換,我正在使用一個函數。 但是,即使內部看起來正確,指針值也會立即變為無效。

這是一個簡單的例子:

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函數內部和外部值之間的差異? 如何糾正?

非常感謝。

您正在將向量按值傳遞給exportVector函數! 函數返回后向量將被破壞,並且您的char *指針變為孤立的,指向的內存位置隨后可能會被重用於其他目的...

另外,您的const_cast應該已經響起了很多警鍾! 切勿執行const_cast (除非您知道這樣做很安全,這是極少數的情況,但這不是其中之一)!

但是這些並不是代碼中唯一的問題。 您如何看待char*** pStr = new char**(); 是在做? 它的作用是為一個指向char的指針(通常為4-8字節)保留一個空間。 您似乎認為以某種方式神奇地為多個字符數組分配了空間?

您需要通過參考傳遞您的載體

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM