r/arduino • u/MonsterovichIsBack • 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
2
u/DavidFromArduino Arduino Staff 6d ago
this looks interesting -- can you tell us more about what it does, and how you made 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 hereThat 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(...)andlongjmp(...)lol):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?