BigBrownBear00
BigBrownBear00

Reputation: 1480

Does ostringstream object need to be cleared?

I am using an ostringstream object in an application that runs for a long time to print debug information to standard out. I toggle whether the application actually prints and clears the ostringstream object based on a command line arg (e.g., verbose flag). When the verbose switch is not asserted I still write to the ostringstream object but I never clear it.

I am trying to figure out how bad this is and whether I should take more care on clearing the object? Are there any negative repercussions such as using too much memory?

// example code 
ostringstream oss;
while (1){
    oss << " still alive " << endl;
    if (verbose) { cout << oss.str(); oss.str("") } 
}

Upvotes: 0

Views: 73

Answers (1)

deW1
deW1

Reputation: 5660

Obviously when you keep inserting data in the stream it'll consume more memory, which at some point can be a lot.

Clearing it will prevent that.

Upvotes: 1

Related Questions