how to pre-allocate memory for a std::string object?
std::string
has a .reserve
method for pre-allocation.
std::string s;
s.reserve(1048576); // reserve 1 MB
read_file_into(s);
When does std::string reallocate memory?
There is no requirement that std::string
release allocated memory when you assign an empty string to it. Nor when you assign a short string to it. The only requirement is that when it allocates memory to hold a larger string, the allocation must be done in a way that achieves amortized constant time. A simple implementation would be to grow by a factor of 2 each time more space is needed.
If you want the string's capacity to be minimized, you can use string::shrink_to_fit()
in C++11. Before C++11 some people resorted to the "swap trick" when they needed to reduce capacity.
Create objects in pre-allocated memory
The two objects are both created at the same memory location, namely buf
. This is undefined behaviour (unless the objects are POD).
If you want to allocate several objects, you have to increment the pointer, e.g. buf + n * sizeof(MyObject)
, but beware of alignment issues
Also don't forget to call destructors when you're done.
Can I pre-allocate a block of memory for C++11 std::tuple?
There's not really an efficient way to do what you want with an std::tuple. They are a compile time construct and you are wanting to determine the types in the tuple at runtime. The only way to do that would be to pre-instantiate every permutation of types up to your maximum number of elements, and that would be rather a large number...
Sounds like what you really want is something like a std::vector<boost::variant<int, double, std::string...>>
.
Two 'new' calls when creating std::string dynamically
new std::string()
This needs two allocations: one for the std::string
object, another for its underlying memory buffer.
If you wrote
std::string s;
you'll see it calls new
once.
Should I preallocate std::stringstream?
Have you profiled your execution, and found them to be a source of slow down?
Consider their usage. Are they mostly for error messages outside the normal flow of your code?
As far as reserving space...
Some implementations probably reserve a small buffer before any allocation takes place for the stringstream. Many implementations of std::string do this.
Another option might be (untested!)
std::string str;
str.reserve(50);
std::stringstream sstr(str);
You might find some more ideas in this gamedev thread.
edit:
Mucking around with the stringstream's rdbuf might also be a solution. This approach is probably Very Easy To Get Wrong though, so please be sure it's absolutely necessary. Definitely not elegant or concise.
C++/CLI: Preallocating memory for string handles
gcnew creates an instance of a managed type on the garbage collected heap. The .NET CLR already preallocates space for the heap and manages its size, and it's pretty smart about it.
You cannot preallocate managed objects. If you want a million managed string objects, you'll need a million gcnew's. On my laptop, this takes a few hundred milliseconds. Is this too slow?
Test your code. If it's actually too slow, maybe you can use a different approach. There's a bit of discussion of alternatives here.
Related Topics
Standard Library Containers with Additional Optional Template Parameters
Child Process Receives Parent's Sigint
Std::Strings's Capacity(), Reserve() & Resize() Functions
Cin for an Int Inputing a Char Causes Loop That Is Supposed to Check Input to Go Wild
C++11: the Range-Based for Statement: "Range-Init" Lifetime
C++ New Operator Thread Safety in Linux and Gcc 4
Emulating Shifts on 32 Bytes with Avx
C++ Memory Model and Race Conditions on Char Arrays
Istream Behavior Change in C++ Upon Failure
Operator Overloading in C++ as Int + Obj
Why Does Ostream_Iterator Not Work as Expected
Unordered_Map Hash Function C++
Why There Is No Placement Delete Expression in C++