1

Keen to use stringstream on an STM32 for handling data that gets sent over various UART connections to radio modules (wifi, BLE, Zigbee, etc). I am running FreeRTOS, if that matters. Very concerned about the dynamic memory allocation in the STL lib causing unhandled overflows (I have to have "-fno-exceptions"). I can handle buffer overruns myself in a simple writer method, so that isn't an issue, but I want to be certain that the stringstream never tries to increase it's memory beyond a pre-set limit.

Currently I just use char* buffers, but, I want to find a better solution.

I want to be able to do something like this.

#include <sstream>      // std::stringstream

class Wifi
{
public:
    std::stringstream* ss;

    Wifi()
    {
        ss.set_max_size(1024); // 1kB
    }
};

TIA

Eliahu Aaron
  • 3,631
  • 2
  • 23
  • 32
  • Maybe [this](https://stackoverflow.com/a/22025719/10927863) answer can help – Eliahu Aaron Jul 30 '19 at 13:36
  • Yes smashing. That means I can use the reserve() method. Much appreciated. Feel free to put as an answer so I can give you the proper credit. Cheers – GreenaGiant Jul 30 '19 at 16:16
  • 1
    Turns out that doesn't work in that you don't have access to the underlying string so you can't use the capacity() method, nor the shrink_to_fit() method. :-( – GreenaGiant Jul 30 '19 at 18:51
  • Basic stringstreams have an allocator template argument. You could try making an allocator that never allocates beyond a boundary, but if I read the [requirements](https://en.cppreference.com/w/cpp/named_req/Allocator) correctly, it relies on exceptions to communicate errors. – PaulR Aug 05 '19 at 08:28

0 Answers0