Trying to understand some best practices for global Strings, not entirely sure the best way handle,
(1) For example to cast and concatenate global text variables as Strings:
String(var1) + ";" + String(var2)
(2) Or to have persistent global Strings outside of function calls:
String someStatus = "Change as needed.."
Above (1) would add / concatenate the 2x global vars (typecast as String) and 1x constant string into 1 String, locally within a function - seems ok. But for above (2) Particle > Code Size Tips says beware of fragmentation with global vars, namely Strings right? So how to handle these?
Some notes, uppercase String is from Spark-Wiring library right? Vs the lowercase string from std:: (standard template library) right? Perhaps both inputs to Particle’s library that has String(), .c_str(), etc. methods no?
So how should text data persist BEYOND a function call, with static local or global vars (ex, in a struct) right? Should you equate them to “ ” (empty) before each assignment? Or perhaps once per hour? But not sure that solves fragmentation as they’d never be deallocated within the app life / run time (or between resets)…
Would it make sense to forgo the strings/Strings entirely and adopt const char *
for globals? Does that solve fragmentation because the pointer is a constant which implies a place in heap memory that never changes? And then use the Particle String methods on these now ‘less functional’ char arrays?
OR considering (const char*)
type casting is used on strings for Log.info (OS examples), can you (const char*)
type cast String globals? But again these global vars could still get fragmented no?
Side note, thought this was a cool thread on determining fragmentation.
Has to be an easy answer here…