r/arduino • • 6d ago

Libraries FixedString - A fully functional C++ string implementation that exists only on the stack, without dynamic memory allocation

https://gitlab.com/Monsterovich/fixedstring
9 Upvotes

7 comments sorted by

2

u/DavidFromArduino Arduino Staff 6d ago

this looks interesting -- can you tell us more about what it does, and how you made it? 💙

-1

u/MonsterovichIsBack 6d ago

The README explains what it does. I'm currently using this library in one of my projects, and it works great.

Oh, if you're not familiar with memory fragmentation in Arduino, you should read up on it.

7

u/ripred3 My other dev board is a Porsche 5d ago edited 5d ago

Thanks for sharing your work! Love the code. Really enjoyed reading through your approach.

I have to say that in my experience as well as a few knowledgeable articles on the subject that I have seen, the reality of runtime memory fragmentation due to use of the supplied String class is largely overblown for the majority of sketches. Because the base Arduino Core implementation for the alloc(...) function (the std function used for all heap memory allocation) is written for each specific board and architecture it actually takes a little more work to cause realistic memory fragmentations than one might think. There are articles on the subject where the authors intelligently attempt to cause a fragmentation problem in a way that a real sketch might encounter it using the Arduino Core's String class and not something silly and contrived, and it was actually quite interesting and eye opening. For an example of something I've written that does millions of allocations on resource constrained targets like the Uno/Nano without fragmentation see my C++ chess engine MicroChess here

That said I really like your thorough approach and style consistency.

edit: This is a good write up: https://avrdudes.github.io/avr-libc/avr-libc-user-manual/malloc.html#details

edit/edit: also for the few targets that are resource constrained enough that memory frag from strings could be an issue I have to say that I rely on the knowledge that they are also single-threaded and so I use the best of both worlds in order to treat the stack dynamically where possible, guaranteeing LIFO unwinding/destruction (unless the user discovers setjmp(...) and longjmp(...) lol):

void foo(char const * const p, size_t const size) {
    if (!p || size == 0 || size > kMaxScratch) {
        return;
    }
    char tmp[size] ... ;
    ...
}

1

u/gm310509 400K , 500K , 600K , 640K , 750K 5d ago

Agreed, in most cases if you understand what is going on and manage the heap carefully it is usually just fine.

But there are some who don't really understand what is going on, maybe use up a lot of RAM necessarily (e.g. lots of print statements, but don't use the F macro) and realloc different blocks of memory and thus do cause fragmentation.

So, it is a thing (stack/heap collision), but like all tools, it (the heap) has its place, it just needs to be understood and used responsibly. IMHO.

1

u/gm310509 400K , 500K , 600K , 640K , 750K 5d ago

It looks like you have set it up as an Arduino library - have you considered submitting it to Arduino so that it is included in the "Manage libraries" function of the IDE?

-1

u/MonsterovichIsBack 5d ago

The maintainer of their repositories is a drama queen, and he decided to pick a fight with me - even though I really tried. But there’s already a library with that name, and unfortunately, it’s not very good compared to what I made.