C ++:退出函数的指针值已更改

时间:2013-11-14 09:56:48

标签: c++ c pointers

我需要从非托管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函数内部及其外部的值的差异?如何纠正?

非常感谢。

2 个答案:

答案 0 :(得分:1)

您正在将值向量传递给exportVector函数!函数返回后,向量被破坏,你的char *指针变成孤立状态,指向可能在以后用于其他目的的内存位置......

此外,你的const_cast应该响起很多警钟!永远不要做const_cast(除了非常罕见的情况,你知道这样做是安全的 - 这不是其中之一)!

但这些事情并不是代码中唯一的问题。您认为char*** pStr = new char**();在做什么? 它的作用是为一个指向char的指针(通常为4-8字节)保留空间。您似乎认为以某种方式神奇地为多个字符数组分配空间?

答案 1 :(得分: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());
}