2

My primary goal is to avoid dynamic memory allocation.

For example, can I be sure which std::string methods will / will not allocate new heap memory?

Is there a way to disallow new allocations by a std::string instance?

Is there a standard fixed length string class?

3
  • 3
    No, you can't be sure. No, there's no way to disallow it (well, not unless you make your own allocator class which doesn't actually allocate anything). And no there's no standard fixed-length string class, but it's quite easy to make one yourself, perhaps wrapping std::array? Commented Sep 10, 2021 at 5:58
  • 3
    No, no any guaranty even with small string optimization inside. std::string is an alias to std::basic_string container. This container have allocator . You can define your own alias with self made allocator which not allocate heap memory using malloc/new, i.e. taking memory from some stack array declared in the top of main function or thread routine. In any case it is going to work for some trivial flow, more less complex programs need heap in C++. Commented Sep 10, 2021 at 6:02
  • IMO the only thing that can be guarantied (by the standard?) is that an empty string (i.e. "\0") will not allocate, which is very useful in itself. Any other claim or dependency on the short string optimization is really just a nice to have detail but it is not fundamental. Commented Sep 13, 2021 at 5:30

2 Answers 2

2

For example, can I be sure which std::string methods will / will not allocate new heap memory?

No, the standard doesn't guaratee non-allocation.

However, you can provide a user defined allocator to std::basic_string. If you don't use dynamic allocation in your custom allocator, then there will be no dynamic allocation.

Is there a standard fixed length string class?

No.

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

3 Comments

Yeah. I thought of this too. Probably best (only) way.
Are there not member functions for which the standard guarantees that no allocation will take place? E.g. indexing an existing string should not re-allocate. What about resizing to a smaller size? There is also the guarantee that appending chars need only log n re-allocations; from that follows that most times, appending one does not re-allocate (even if there aren't any guarantees just which appending will trigger one), etc.
@Peter-ReinstateMonica There are technically no guarantees that the default constructor won't allocate, so if you go by guarantees, then you're stuck from the start.
1

Is there a way to disallow new allocations by a std::string instance?

No, but instead we can use std::pmr::string since c++17 (If the string is guaranteed to be small enough: say shorter than 16bytes, we may benefit from the std::string's short string optimization in recent version STLS then memory allocation would not happen, but it's a black-box)

constexpr size_t kMaxLen = 256; char buffer[kMaxLen] = {}; std::pmr::monotonic_buffer_resource pool{std::data(buffer),std::size(buffer)}; std::pmr::string vec{&pool}; // assert that we don't have a string with a length larger than kMaxLen - 1, or we will trigger heap memory allocations 

Is there a standard fixed-length string class?

NO, but we may choose non standard libraries as the alternatives:

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.