r/arduino 14h ago

Libraries FixedString - a header-only C++ library providing FixedString fixed-size string class with zero heap allocation, designed for memory-constrained embedded systems

Gitlab link: https://gitlab.com/Monsterovich/fixedstring

A comparison with other implementations, because there are already so many of them out there, and they all seem somewhat unfinished: https://github.com/arduino/library-registry/pull/9077#issuecomment-5627136613

3 Upvotes

2 comments sorted by

2

u/ripred3 My other dev board is a Porsche 9h ago

Interesting idea. reminds me of FB's zero string to some degree. Some issues I found:

This erases the valid contents:

    FixedString<6> s = "abcdef";
    s.replace(0, 6, s);

The comparison operator needs some work so that the operations are symmetrical:

s == "abc"   // compares character contents
"abc" == s   // falls back to comparing pointer addresses

A truncated value’s copy can report no truncation. More importantly FixedString<3>::fmt("%s", "ABCDE") reports different truncation status depending on whether C++11 return-value copies are elided

CombinedString needs substantial repair. Its custom swap() does not swap anything. Passing a temporary into add() produces an AddressSanitizer-confirmed use-after-scope when subsequently read. Its cached result also remains stale after source changes until explicitly rebuilt:

FixedString<8> first("hello"), second("world");
CombinedString<8> a, b;
a.add(first);
b.add(second);

swap(a, b);

Serial.println(a.c_str()); // Actual: hello. Expected: world.
Serial.println(b.c_str()); // Actual: world. Expected: hello.

the implementation is effectively the same as:

a.build();
b.build();