Stack-based string

Is there an existing real-time-safe string class/library that people are using?

Probably depends on what you need. std::string_view is a realtime safe way to pass around strings or substrings of string instances which have already been created outside of the realtime safe context or which are compile time constants.

If you need to create and manipulate strings from a realtime thread, std::pmr::string in combination with a std::pmr::monotonic_buffer_resource could be an option, I haven’t used std::pmr::string but dynamic std::pmr::vector instances in a real-time safe context with the memory resources from GitHub - cradleapps/realtime_memory: A backfill for some C++17 pmr memory resources suitable for realtime use on macOS.

2 Likes

If you know your strings are going to be relatively short, you might be able to do something with std::array<char, max_string_size> and std::string_view. I haven’t tried it, but it seems like it might work.

Thanks for the info, guys! The library looks like it could come in handy one day. On second look, however, I think I’ll get away with C-style strings… I did start writing a constexpr limited-size string class that looks like it could be quite nice, but I suppose its niche would be very small.

Just like std::vector, std::string has a reserve() method.

You can use that to preallocate whatever size you need during construction.

1 Like

Good point, I’ll bare that in mind!

You could also look into custom allocators, and use some sort of pool allocator for the cases in which you don’t know the string size at build time. The specifics depend on the exact scenario you’re trying to solve of course.

Looks like Boost has it covered:
https://www.boost.org/doc/libs/1_85_0/libs/static_string/doc/html/static_string/ref/boost__static_strings__basic_static_string.html