I've tried several things already,
std::stringstream m;
m.empty();
m.clear();
both of which don't work.
I've tried several things already,
std::stringstream m;
m.empty();
m.clear();
both of which don't work.
For all the standard library types the member function empty()
is a query, not a command, i.e. it means "are you empty?" not "please throw away your contents".
The clear()
member function is inherited from ios
and is used to clear the error state of the stream, e.g. if a file stream has the error state set to eofbit
(end-of-file), then calling clear()
will set the error state back to goodbit
(no error).
For clearing the contents of a stringstream
, using:
m.str("");
is correct, although using:
m.str(std::string());
is technically more efficient, because you avoid invoking the std::string
constructor that takes const char*
. But any compiler these days should be able to generate the same code in both cases - so I would just go with whatever is more readable.
m.str(std::string());
throw? –
Papistry m.str("foo"); m.str << "bar"
does not return "foobar" –
Inappreciative You can clear the error state and empty the stringstream all in one line
std::stringstream().swap(m); // swap m with a default constructed stringstream
This effectively resets m to a default constructed state, meaning that it actually deletes the buffers allocated by the string stream and resets the error state. Here's an experimental proof:
int main ()
{
std::string payload(16, 'x');
std::stringstream *ss = new std::stringstream; // Create a memory leak
(*ss) << payload; // Leak more memory
// Now choose a way to "clear" a string stream
//std::stringstream().swap(*ss); // Method 1
//ss->str(std::string()); // Method 2
std::cout << "end" << std::endl;
}
When the demo is compiled with address sanitizer, memory usage is revealed:
=================================================================
==10415==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 392 byte(s) in 1 object(s) allocated from:
#0 0x510ae8 in operator new(unsigned long) (/tmp/1637178326.0089633/a.out+0x510ae8)
#1 0x514e80 in main (/tmp/1637178326.0089633/a.out+0x514e80)
#2 0x7f3079ffb82f in __libc_start_main /build/glibc-Cl5G7W/glibc-2.23/csu/../csu/libc-start.c:291
Indirect leak of 513 byte(s) in 1 object(s) allocated from:
#0 0x510ae8 in operator new(unsigned long) (/tmp/1637178326.0089633/a.out+0x510ae8)
#1 0x7f307b03a25c in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::reserve(unsigned long) (/usr/local/lib64/libstdc++.so.6+0x13725c)
#2 0x603000000010 (<unknown module>)
SUMMARY: AddressSanitizer: 905 byte(s) leaked in 2 allocation(s).
Pretty steep if you ask me. To hold just 16bytes of payload, we spent 905 bytes ... string streams are no toy. Memory is allocated in two parts:
If you enable method 1 (the one shown in this answer) the extra 513 (payload) bytes are reclaimed, because the stream is actually cleared.
If you enable method2 as suggested in the comments or other answers, you can see that all 905 bytes are in use by the time we exit.
In terms of program semantics, one may only care that the stream "appears" and "behaves" as empty, similar to how a vector::clear
may leave the capacity untouched but render the vector empty to the user (of course vector would spend just 16 bytes here). Given the memory allocation that string stream requires, I can imagine this approach being often faster. This answer's primary goal is to actually clear the string stream, given that memory consumption that comes with it is no joke. Depending on your use case (number of streams, data they hold, frequency of clearing) you may choose the best approach.
Finally note that it's rarely useful to clear the stream without clearing the error state and all inherited state. The one liner in this answer does both.
m.str({})
, as Zhang's comment said. To clarify: When you call m.str({})
I would expect that it would reuse some of the memory it allocated during the first operation to speed up the next operation. When you call std::stringstream().swap(m)
I expect any memory it allocated during the first operation would have its ownership transferred to the temporary you just created, losing the chance for that efficiency. It would interesting to see a comparison in practice though. –
Mississippian m = std::stringstream();
rather than swapping with an object you're deleting –
Pyrargyrite m
's memory address will be left in the memory without destruction. The stringstream()
construct creates a new object and swaps it with m
. –
Theadora swap
is a good general solution because streams can have a lot of internal state. Regarding temporaries, I would expect the compiler to eliminate them. And allocation is fraught; eschew pre-optimization. I would expect allocation to typically be pointer fiddling. –
Galleon m
gets swapped into the unnamed temporary, which is destructed at the end of the statement and thus frees all resources previously owned by m
. Or if you meant the assignment from a default-constructed instance, then the assignment operator is specified not to leak (because why would it?). Nothing is "left in the memory without destruction". C++ with its RAII ensures things are destroyed automatically when appropriate, unless you go out of your way to allocate manually and don't free, but you should just not do that. –
Wailoo m.str(std::string())
version. However, I left out the m.clear()
, so that could also be a contributor –
Shanna reset
function (yet). I've updated my answer to hint at more factors that may affect your choice. As stated among others it's hardly useful to clear the stream without clearing the error state. Then there's the memory consideration. –
Casper m.str("")
, just remember to clear the state, this may happen if you don't (when I say state, I mean the error state and all inherited state - I couldn't guarantee those two are one and the same). I don't have extra remarks on the "whole point", all I (re)searched is shown in the answer. –
Casper This should be the most reliable way regardless of the compiler:
m=std::stringstream();
mm.clear(); mm.str("");
did the trick. (no C++11, else swap would be better). –
Griddle swap
be better than move-assignment? –
Madonna use of deleted function ‘std::basic_stringstream<char>& std::basic_stringstream<char>::operator=(const std::basic_stringstream<char>&)
–
Esther m.str("");
seems to work.
I am always scoping it:
{
std::stringstream ss;
ss << "what";
}
{
std::stringstream ss;
ss << "the";
}
{
std::stringstream ss;
ss << "heck";
}
stringstream?
–
Tropine my 2 cents:
this seemed to work for me in xcode and dev-c++, I had a program in the form of a menu that if executed iteratively as per the request of a user will fill up a stringstream variable which would work ok the first time the code would run but would not clear the stringstream the next time the user will run the same code. but the two lines of code below finally cleared up the stringstream variable everytime before filling up the string variable. (2 hours of trial and error and google searches), btw, using each line on their own would not do the trick.
//clear the stringstream variable
sstm.str("");
sstm.clear();
//fill up the streamstream variable
sstm << "crap" << "morecrap";
There are many other answers that "work", but they often do unnecessary copies or reallocate memory.
Swapping streams means that you need to discard one of them, wasting the memory allocation. Same goes for assigning a default-constructed stream,
Assigning to the string in the string buffer (via stringstream::str
or stringbuf::str
) may lose the buffer already allocated by the string.
The canonical way to clear the string stream would be:
void clear(std::stringstream &stream)
{
if (stream.rdbuf()) stream.rdbuf()->pubseekpos(0);
}
The canonical way to get the size of the data in the stream's buffer is:
std::size_t availSize() (const std::stringstream& stream)
{
if (stream.rdbuf())
return std::size_t(
stream.rdbuf()->pubseekoff(0, std::ios_base::cur, std::ios_base::out));
else
return 0;
}
The canonical way to copy the data from the stream to some other preallocated buffer and then clear it would then be:
std::size_t readAndClear(std::stringstream &stream, void* outBuf, std::size_t outSize)
{
auto const copySize = std::min(availSize(stream), outSize);
if (!copySize) return 0; // takes care of null stream.rdbuf()
stream.rdbuf()->sgetn(outBuf, copySize);
stream.rdbuf()->pubseekpos(0); // clear the buffer
return copySize;
}
I intend this to be a canonical answer. Language lawyers, feel free to pitch in.
@Azeem's answer should be considered the best solution here. On the one hand his answer doesn't address the specific words of the OP's question, but on the other hand I think it addresses the intent of the OP's question. Excerpting @Azeem's code:
{
std::stringstream ss;
ss << "what";
}
Benefits of using scoping contexts:
Drawbacks:
It's a conceptual problem.
Stringstream is a stream, so its iterators are forward, cannot return. In an output stringstream, you need a flush() to reinitialize it, as in any other output stream.
These do not discard the data in the stringstream in gnu c++
m.str("");
m.str() = "";
m.str(std::string());
The following does empty the stringstream for me:
m.str().clear();
© 2022 - 2024 — McMap. All rights reserved.