0

I have certain string processing to do , the approach I am using is in following sample -

Void ProcessObjects(int nObject)
{
    std::string sInfostr;

    for(int i = 0;i<nObject;i++)
    {
      InfoObject Inf = new InfoObject; 
      GetInfoObject(&Inf);
      GetStoredInformation(Inf, std::string &sInfostr)
      delete Inf;           
    }
}


void GetStoredInformation(InfoObject Inf, std::string &sInfostr) 
{
    char tag[1000]; 

    GetInformation(&Inf);

    sprintf(tag, "name=%s",Inf.name);
    sInfostr += tag;        
    sprintf(tag, "name1=%s",Inf.name1);
    sInfostr += tag;
    sprintf(tag, "name2=%s",Inf.name2);
    sInfostr += tag;
    sprintf(tag, "name3=%s",Inf.name3);
    sInfostr += tag;
    sprintf(tag, "name4=%s",Inf.name4);
}

Now can I get some suggestion is it a good way to process string? Will I code go in any trouble if "nObject" above 10,000?

4
  • 2
    Why aren't GetInfObject() and GetStoredInformation() methods of InfoObject? Commented Oct 10, 2011 at 8:35
  • 1
    Why not declare name, name1.... name4 all as std::string? Commented Oct 10, 2011 at 8:37
  • "the approach I am using is in following sample". No it is not. This code is littered with syntax errors. Please post a working sample. Commented Oct 10, 2011 at 8:56
  • newing and deleting an object in the loop seems a bit unnecessary. You could just as well use an automatic object, possible hoisted out of the loop. - Also usage like InfoObject Inf = GetInfoObject(); is simpler and probably also more efficient than what you are doing. Commented Oct 10, 2011 at 9:34

4 Answers 4

1

I suggest you use std::istringstream instead of the old C-ish sprintf:

void GetStoredInformation(InfoObject Inf, std::string &sInfostr) 
{
    GetInformation(&Inf);
    std::istringstream stream();
    stream << "name=" << Inf.name
           << "name1=" << Inf.name1
           << "name2=" << Inf.name2
           << "name3=" << Inf.name3
           << "name4=" << Inf.name4;
    sInfostr = stream.str();
}

Will I code go in any trouble if "nObject" above 10,000?

Depends on your used hardware, from the C++ point of view it should not be a problem, unless your InfoObject is not extremely large.

Sign up to request clarification or add additional context in comments.

Comments

1

Your code is very sketchy, and contains several syntax errors and probably a memory leak.

About your actual question, using std::stringstream is the recommended way to build strings. It would look like this:

std::stringstream buffer;
buffer << "name=" << Inf.name
    << "name1=" << Inf.name1; // etc.

You can access the contents of buffer as an std::string using buffer.str().

Aside from being more elegant, this is probably also faster, because std::stringstream uses a smarter allocation-strategy than operator += of std::string.

2 Comments

Are you sure. The last time I measured (some time ago), the implementation of std::ostringstream I used ended up just doing += on a std::string. In this particular case (just appending strings), std::string would be simpler and more rapid. (Use ostringstream, of course, if you have to convert anything.)
@James: To be honest, I am not sure, since I have not measured.
1

If you declare name, name1.... name4 all as std::string, then you would simply write:

sInfostr = "name  = " + Inf.name 
         + "name1 = " + Inf.name1 
         + "name2 = " + Inf.name2
         + "name3 = " + Inf.name3 
         + "name4 = " + Inf.name4;

which is much neater solution, as it is easier to read and maintain.

You can add member function called ToString() to InfoObject class as:

class InfoObject
{
  //...
  std::string ToString() const
  {
     return  = "name  = " + Inf.name 
             + "name1 = " + Inf.name1 
             + "name2 = " + Inf.name2
             + "name3 = " + Inf.name3 
             + "name4 = " + Inf.name4;
  }
};

Then GetStoredInformation would be just two lines:

void GetStoredInformation(InfoObject Inf, std::string &sInfostr) 
{
    GetInformation(&Inf);
    sInfostr = Inf.ToString();
}

Even better would be if you make GetStoredInformation a member function of InfoObject.

Comments

0

You should not use sprintf at all. As you suspect, it is not safe. If any of the Inf.name* strings are longer than 1000 characters, you will corrupt the stack which can cause a crash or worse, a security breach via code injection.

Your format string is simple enough; the solution should be more like,

void GetStoredInformation(InfoObject Inf, std::string &sInfostr) 
{
    GetInformation(&Inf);
    sInfostr += "name=";// did you mean sInfostr = "name"?
    sInfostr += Inf.name;
    sInfostr += "name1=";
    sInfostr += Inf.name1;
    sInfostr += "name2=";
    sInfostr += Inf.name2;
    sInfostr += "name3=";
    sInfostr += Inf.name3;
    sInfostr += "name4=";
    sInfostr += Inf.name4;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.