Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 401619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:03:02+00:00 2026-05-12T17:03:02+00:00

Lets say that I’m trying to solve a parsing problem of string to char

  • 0

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?

  1. Here’s what it’s supposed to do :
  2. Dump all argv into a string_array
    container
  3. Dump everything in the string_array
    container into a std::string and
    separate with spaces
  4. Break the string down into string
    tokens using boost/algorithm/string
  5. 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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T17:03:02+00:00Added an answer on May 12, 2026 at 5:03 pm

    C-style strings (char*) are meant to be zero-terminated. So instead of new char[tokens[i].size()], you need to add 1 to the allocation: new char[token[i].size() + 1]. Also, you need to set new_args[i][tokens[i].size()] = 0 to zero-terminate the string.

    Without the zero-terminator, programs would not know when to stop printing, as char* does not hold a string length, unlike std::string.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say that I'm trying to create a new process with the following code:
Lets say that I have the following code: public class Shelter<A extends Animal, B
Lets say that I have this code $(document).ready(function() { $(#changeText).click(function() { $(#textBox).html(My<br><br><br>text<br><br><br>is<br><br><br>changed!); }); });
Lets say I have the following code: public class Base { // Some stuff
Lets say that in a browser based game, completing some action (for simplicity lets
Is that even possible ? Lets say that the code has a lot of
Lets say that me and my friend are trying to work on same software
Lets say that I have this code: <div id=gameFrame> <div id=figure></div> </div> Where the
Lets say that I have a generic collection in the string format. I want
Lets say that i have a class with some instance variables and i want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.