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 8140191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:04:47+00:00 2026-06-06T12:04:47+00:00

I have a class with parse(int argc, char* argv[]) function which I have to

  • 0

I have a class with parse(int argc, char* argv[]) function which I have to use to set a desired state of an object. I’m taking the parameters from the gui using stringstream and then I’m trying to convert them to char** to pass them to the function. Here’s what I’ve got:

std::stringstream sstream;

sstream << "-clip" << " " << min_x_entry.get_text()
        << " " << max_x_entry.get_text(); // etc.

std::cout << sstream.str();    // All looks good here

std::vector<std::string> args;
std::vector<char*> argv;
std::string arg;

while (sstream >> arg)
{
    args.push_back(arg);
    argv.push_back(const_cast<char*>(args.back().c_str()));
}
argv.push_back(0);

int argc = args.size();

for (int i = 0; i < argc; ++i)
    std::cout << &argv[0][i];    // This outputs garbage

my_object.parse(argc, &argv[0])  // And this fails

What am I missing? Is there a better way of achieving this?

  • 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-06-06T12:04:48+00:00Added an answer on June 6, 2026 at 12:04 pm

    A problem would be reallocation of the args vector as push_back() will grow the size of the vector if required:

    If new size() is not larger than capacity(), no iterators or references are invalidated. Otherwise all iterators and references are invalidated.

    The argv vector is storing pointers to the internals of elements in args, so these would be invalidated.

    A solution would be to create the args vector first then create the argv vector afterwards:

    while (sstream >> arg) args.push_back(arg);
    
    for (auto i = args.begin(); i != args.end(); i++)
    {
        argv.push_back(const_cast<char*>(i->c_str()));
    }
    argv.push_back(0);
    

    The for loop that prints out the argv strings is incorrect. This:

    &argv[0][i]
    

    is a char* but starts from ith element of the first entry in argv. For example, if the first c-string in argv was "string":

    &argv[0][1] is "tring"
    &argv[0][2] is "ring"
    

    change to:

    for (int i = 0; i < argc; i++)
        std::cout << argv[i] << std::endl; // Added 'endl' to flush 'cout'.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Login class that will parse an xml sent from an iOS,
I have a class in a dll which parses a file and returns a
I have class Building in which i have one class member NSMutableArray *subnode.I have
I have this var result = general.GetInformation(int.Parse(ID), F_account, F_Info, Types); this GetInformation is my
let's say i have a class: [Serializable] public sealed class MyFoo { public int
I have a object cache class like this: #include boost/thread/mutex.hpp #include boost/unordered_map.hpp template <typename
I have a class that I use to setup objects in an array. In
I have an application that's instantiates an object from a class called SecretNumber when
I have a class in Java which is written bellow.I want to know does
I have a class from which I want to get values from combo box

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.