Using the following code mentioned in https://stackoverflow.com/a/236803/6361644, I wrote the following code to parse a string into a vector, where each element is separated by white space.
std::string line = "ls -l -a";
std::string cmd;
std::vector<char*> argv;
std::stringstream ss;
ss.str(line);
std::string tmp;
getline(ss, cmd, ' ');
argv.push_back( const_cast<char*>(cmd.c_str() ) );
while(getline(ss, tmp, ' '))
argv.push_back( const_cast<char*>(tmp.c_str() ) );
argv.push_back(NULL);
Printing argv after this code gives
{gdb) print argv
$22 = std::vector of length 3, capacity 4 = {0x26014 "ls", 0x2602c "-a", 0x2602c "-a", 0x0}
I'm not sure why the second element is being overwritten. Any tips would be appreciated.