std::strstreambuf::str(3)
π
std::strstreambuf::str β C++ Standard Libary (3) β¨
okay bestie, welcome to the <pink>C++ Standard Libary</pink> page for std::strstreambuf::str(3) β this one is a whole little cursed-but-iconic legacy vibe, and we're gonna read it together. π«Ά
πΈ NAME πΈ
std::strstreambuf::str β std::strstreambuf::str. yes, that's the name, she's serving "function that is literally named after herself" energy π
β¨ Synopsis β¨
the signature, preserved with love:
char* str(); (deprecated in C++98)
(removed in C++26)
so here's what she does: calls freeze(), then returns a copy of the start pointer of the get area, std::streambuf::eback(). π§
and for all writeable std::strstreambuf objects constructed through the interface provided by std::strstream, the start of the get area is also the start of the put area. iykyk π«
π Parameters π
- (none) β literally not a single parameter, zero, nada, the void is empty and slaying π
π Return value π
a copy of eback(), which may be a null pointer. so yes, bestie, check it before you wreck it β null is on the menu π
π₯ Notes π₯
this function is typically called through the std::strstream interface.
the call to freeze() guarantees that the returned pointer remains valid until the next explicit call to freeze(false): otherwise (on a dynamic buffer) any output operation could trigger buffer reallocation which would invalidate the pointer. that's the whole "don't let the buffer move while you're holding the pointer" situation, fr fr π«Ά
π¨ warning bestie: it also causes a memory leak in the destructor ofstd::strstreambuf, unlessfreeze(false)is called before the buffer (or, more commonly, thestd::strstreamthat manages it) is destroyed. not me warning you about a leak and then still thinking the example is cute π
π¦ Example π¦
the page gives you a // Run this code moment, and the code is:
#include <iostream>
#include <strstream>
int main()
{
std::strstream dyn; // dynamically-allocated read/write buffer
dyn << "Test: " << 1.23 << std::ends;
std::strstreambuf* buf = dyn.rdbuf();
std::cout << "R/W buffer holds [" << buf->str() // or dyn.str()
<< "]\n";
dyn.freeze(false); // after calling .str() on a dynamic strstream
char arr[10];
std::ostrstream user(arr, 10); // fixed-size write-only buffer
buf = user.rdbuf();
user << 1.23 << std::ends;
std::cout << "Write-only buffer holds [" << buf->str() // or
user.str()
<< "]\n";
std::istrstream lit("1 2 3"); // fixed-size read-only buffer
buf = lit.rdbuf();
std::cout << "Read-only buffer holds [" << buf->str() // or
lit.str()
<< "]\n";
}
β Output β
R/W buffer holds [Test: 1.23]
Write-only buffer holds [1.23]
Read-only buffer holds [1 2 31 2 3]
π See also π
straccesses the output buffer β public member function ofstd::strstreamstraccesses the output buffer β public member function ofstd::ostrstreamstraccesses the output buffer β public member function ofstd::istrstream
mwah π β source: http://cppreference.com, date: 2024.06.10, page: std::strstreambuf::str(3). byeee, go call freeze(false) and hydrate ππ«Άβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms