简体   繁体   中英

C++: Pointer value changed exiting a function

I need to wrap a string vector from an unmanaged C++ library, and to do so I have to convert a vector<string> to a char*** . For this conversion, I'm using a function. However, even if it seems right inside it, once out pointer values changed to be invalid.

Here is a simple example of it:

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

And I'm getting in console:

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  

How to explain the difference of values inside exportVector function and outside it? How to correct it?

Thanks a lot.

You are passing the vector by value to the exportVector function! The vector gets destructed after the function returns, and your char* pointers get orphaned, pointing to memory locations which probably are reused for other purposes afterwards...

Also, your const_cast should ring a lot of alarm bells already! Never do a const_cast (except for very rare occasions where you know it is safe to do so - and this is not one of them)!

But those things are not the only problems in your code. What do you think char*** pStr = new char**(); is doing? What it does is that it reserves space for one pointer to a pointer to a char (usually 4-8 byte). You seem to think that somehow magically allocates space for a multitude of character arrays?

You need to pass your vector by reference

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

,also you should use strdup here:

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

finally,

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

will segfault cause charVect will be destroyed at the end of the function, you need to remove those line and do something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM