I am fairly new to c and Arduino. On other non Arduino micros I am able to save memory space by putting strings in constants and/or variables and save several bytes of program memory. When I attempt to do the same, I consume more memory. Why?
Here are two examples I am using as an example. The first is how most users display strings on serial console. The second should technical save about 96 bytes of memory but it consumes KB more memory.
Program 1
void setup() {
Serial.begin(115200); // open the serial port
Serial.print("The Arduino microcontroller is a fun microcontroller to learn to program in C.\n");
Serial.print("The Arduino microcontroller has 32kb storage for programs.\n");
Serial.print("The Arduino microcontroller is a microcontroller that helps people learn electronics as well.\n");
}
void loop() {
// put your main code here, to run repeatedly:
}
Program 2
void setup() {
Serial.begin(115200); // open the serial port
String sArduino = "Arduino"; // 7 bytes
String sMicrocontroller = "microcontroller"; // 15 bytes
Serial.print("The " + sArduino + " " + sMicrocontroller + " is a fun " + sMicrocontroller + " to learn to program in C.\n");
Serial.print("The " + sArduino + " " + sMicrocontroller + " has 32kb storage for programs.\n");
Serial.print("The " + sArduino + " " + sMicrocontroller + " is a " + sMicrocontroller + " that helps people learn electronics as well.\n");
// total savings should be 96 bytes
}
void loop() {
// put your main code here, to run repeatedly:
}
Am I not declaring the strings properly or will the total equivalent string data need to have significantly more text to show any savings. Other micros, non c, are ably to show a savings of memory/flash. Am I doing this wrong in C?
Thank you in advance for your response!
Why not simply use the "F" macro? instead of:
Serial.print("The Arduino microcontroller is a fun microcontroller to learn to program in C.\n");
Serial.print(F("The Arduino microcontroller is a fun microcontroller to learn to program in C.\n"));
That is all there is to it.
Ive thought and tried the same thing and nothing much changes. I figured it was because of some block allocation concept in use such that you have save a whole block before seeing a change.
Your second example uses the String class, which adds a considerable amount of code that must then be stored in the flash (program) memory. Using the F() macro with the code from the first example will save a considerable amount of dynamic memory, but use a bit more flash memory because of the extra code needed by the print function to handle strings stored in flash memory.
You can optimize it a bit because of the identical text at the start of each line, but not really worth the effort unless you are drastically short on memory.
void setup() {
static const char text1[] PROGMEM = "The Arduino microcontroller ";
Serial.begin(115200); // open the serial port
Serial.print((__FlashStringHelper*)text1);
Serial.print(F("is a fun microcontroller to learn to program in C.\n"));
Serial.print((__FlashStringHelper*)text1);
Serial.print(F("has 32kb storage for programs.\n"));
Serial.print((__FlashStringHelper*)text1);
Serial.print(F("is a microcontroller that helps people learn electronics as well.\n"));
}
void loop() {
// put your main code here, to run repeatedly:
}
I tested it and it does work, but what on earth is __FlashStringHelper* and where does it come from and are there any more of them?
sevenoutpinball:
I tested it and it does work, but what on earth is __FlashStringHelper* and where does it come from and are there any more of them?
It's just a pointer to a specific data type, so that the print routines can be overloaded to cope with strings in flash memory.
is there any advantage to using something like this
#include <iostream>
using namespace std;
class printData {
public:
void print(int i) {
cout << "Printing int: " << i << endl;
}
void print(double f) {
cout << "Printing float: " << f << endl;
}
void print(char* c) {
cout << "Printing character: " << c << endl;
}
};
int main(void) {
printData pd;
// Call print to print integer
pd.print(5);
// Call print to print float
pd.print(500.263);
// Call print to print character
pd.print("Hello C++");
return 0;
}
here in arduinoland?