This is similar to: Is this function subject to memory fragmentation?
I am trying to get my head around Memory fragmentation in Arduino's and embedded systems in general.
Currently I have a function in a __lcdAux__
Class which is responsible for doing basic operation on the lcd.
One of the members of the class is a char array double2StrBuff1
which is used with __dtostrf()__
to create a double which is then passed to sprintf
. So the following function (__lcdAux::UpdatePosition__
) is responsible for updating the position on the lcd.
void lcdAux::UpdatePosition(double MtXPosLengthUnits) {
lcd->setCursor(0, 2);
dtostrf(MtXPosLengthUnits, 0,3, double2StrBuff1);
sprintf(lcdBuff, "X: %s", double2StrBuff1);
lcd->print(lcdBuff);
}
Currently, I am under the impression that by defining once the class as a global variable and then using it throughout the program, then the char array is allocated only once memory space. Therefore by using it afterwards no memory fragmentation occurs.
However, I read that functions allocate space on the stack which is immediately recoved when the function is exited. Therefore I am curious if the following declaration is any different in terms of memory fragmentation.
void lcdAux::UpdatePosition(double MtXPosLengthUnits) {
lcd->setCursor(0, 2);
char double2StrBuff1[10];
dtostrf(MtXPosLengthUnits, 0,3, double2StrBuff1);
sprintf(lcdBuff, "X: %s", double2StrBuff1);
lcd->print(lcdBuff);
}
To sum up:
- by putting the declaration for variable
__char double2StrBuff1[10];__
inside the function, is the memory allocated from the stack and therefore recovered upon exiting the function (therefore there is no need to declarate a member variable on the class level),
OR
- is the fact that
__double2StrBuff1__
is a pointer means that the pointer is allocated in the stack but the values are located in the heap (therefore what I am doing makes sense)?
PS: Because some might point out that __"X: %s"__
is worse in terms of memory usage and fragmentation, I'll state the following. Instead of the format string __"X:%s"__
I use a _PROGMEM
const char_ array
and __strcpy_P()_
, but to simplify the question I replaced it with the literal string , since I feel that is another problem.