Lets say that I’m trying to solve a parsing problem of string to char **
For some reason the below code generates a lot of trash, can anyone have a look at it please?
- Here’s what it’s supposed to do :
- Dump all argv into a string_array
container - Dump everything in the string_array
container into a std::string and
separate with spaces - Break the string down into string
tokens using boost/algorithm/string - create a new char ** and dump all
tokens into it, print out the new char **, clean up
What have I done wrong ?
#include <string>
#include <vector>
#include <iostream>
#include <boost/algorithm/string.hpp>
using namespace std;
using namespace boost;
typedef vector<string> string_array;
int main(int argc, char ** argv)
{
string_array args;
string_array tokens;
cout << "Real arguments :" << endl;
for(int i = 0; i < argc; i++)
{ cout << argv[i] << endl;}
string arg = "";
for(int i = 1; i < argc; i++)
{
args.push_back(argv[i]);
}
for(int i = 0; i < (int)args.size(); i++)
{
arg += args[i];
if(i != (int)args.size() - 1)
arg += " ";
}
split(tokens, arg, is_any_of(" "));
char ** new_args = NULL;
new_args = new char*[(int)tokens.size()];
for(int i = 0; i < (int)tokens.size(); i++)
{
new_args[i] = new char[(int)tokens[i].size()];
for(int j = 0; j < (int)tokens[i].size(); j++)
{
new_args[i][j] = tokens[i][j];
}
}
for(int i = 0; i < (int)tokens.size(); i++)
{ std::cout << new_args[i] << std::endl; }
delete [] new_args;
}
C-style strings (
char*) are meant to be zero-terminated. So instead ofnew char[tokens[i].size()], you need to add 1 to the allocation:new char[token[i].size() + 1]. Also, you need to setnew_args[i][tokens[i].size()] = 0to zero-terminate the string.Without the zero-terminator, programs would not know when to stop printing, as
char*does not hold a string length, unlikestd::string.