I noticed a weird behaviour programming the Pico with C++. I'm doing some string operations and concatenations. And everything is fine with the equivelent test program on the PC. But on the Pico weird things happen. Either, strings are empty, or the contain things they shouldn't (can't!) contain. It took me a while to realize that the problem might be due to a memory problem, perhaps a fragmented RAM? I'm not sure about that, but if I do the operations on a very short string, everything is ok, even on the Pico. But after extending it a little bit, it goes crazy again.
So the question is, is it likely that the reason is fragmented memory? I think of reallocations when for example chars are added to a string. I'm not very experienced with microcontrollers, perhaps this is a well known issue? Is it perhaps adviseable or necessary to avoid using std::string in embedded programming? Perhaps someone could tell me whether my assumption of fragmented memory is reasonable, or how I could tackle it down, or even what could I do against it. Working with C-strings only?
By the way, this is the part of program which works flawlessly on my PC machine. But integrating it on the Pico results in the problems described above:
So the question is, is it likely that the reason is fragmented memory? I think of reallocations when for example chars are added to a string. I'm not very experienced with microcontrollers, perhaps this is a well known issue? Is it perhaps adviseable or necessary to avoid using std::string in embedded programming? Perhaps someone could tell me whether my assumption of fragmented memory is reasonable, or how I could tackle it down, or even what could I do against it. Working with C-strings only?
By the way, this is the part of program which works flawlessly on my PC machine. But integrating it on the Pico results in the problems described above:
Code:
#include <algorithm>#include <iostream>#include <map>#include <regex>#include <string>std::map<char, std::string> morseCode = { {'A', ".-"}, {'B', "-..."}, {'C', "-.-."}, {'D', "-.."}, {'E', "."}, {'F', "..-."}, {'G', "--."}, {'H', "...."}, {'I', ".."}, {'J', ".---"}, {'K', "-.-"}, {'L', ".-.."}, {'M', "--"}, {'N', "-."}, {'O', "---"}, {'P', ".--."}, {'Q', "--.-"}, {'R', ".-."}, {'S', "..."}, {'T', "-"}, {'U', "..-"}, {'V', "...-"}, {'W', ".--"}, {'X', "-..-"}, {'Y', "-.--"}, {'Z', "--.."}, {'1', ".----"}, {'2', "..---"}, {'3', "...--"}, {'4', "....-"}, {'5', "....."}, {'6', "-...."}, {'7', "--..."}, {'8', "---.."}, {'9', "----."}, {'0', "-----"}, {',', "--..--"}, {'?', "..--.."}, {'/', "-..-.-"}, {'=', "-...-"}, // '=' == <BT> {'k', "-.--."}, // k == <KN> {'s', "...-.-"}, // s == <SK> {'+', ".-.-."}, // + == <AR> {'a', "-.-.-"}, // a == <KA>};void messageToMorse(const std::string &msg) { std::string msgUpper; std::string morse; msgUpper.resize(msg.length()); std::transform(msg.cbegin(), msg.cend(), msgUpper.begin(), [](unsigned char c) { return std::toupper(c); }); // Encode the special characters as we like it msgUpper = std::regex_replace(msgUpper, std::regex("<BT>"), "="); msgUpper = std::regex_replace(msgUpper, std::regex("<KN>"), "k"); msgUpper = std::regex_replace(msgUpper, std::regex("<SK>"), "s"); msgUpper = std::regex_replace(msgUpper, std::regex("<AR>"), "+"); msgUpper = std::regex_replace(msgUpper, std::regex("<KA>"), "a"); // Remove all other unknown characters msgUpper.erase(remove_if(msgUpper.begin(), msgUpper.end(), [](const char &c) { return c != ' ' && morseCode.find(c) == morseCode.end(); }), msgUpper.end()); // Remove spaces, if there are too many of them msgUpper = std::regex_replace(msgUpper, std::regex("(\\s+)"), " "); for (int i = 0; i < msgUpper.length(); i++) { auto c = msgUpper[i]; if (c == ' ') { morse += 'w'; continue; } // Ignore and continue with next char, if not found auto search = morseCode.find(c); if (search == morseCode.end()) { continue; } for (int j = 0; j < morseCode[c].length(); j++) { auto m = morseCode[c][j]; if (j == 0 && i > 0 && msgUpper[i - 1] != ' ') { morse += 'c'; } morse += m; if (j < morseCode[c].length() - 1) { morse += 'i'; } } } // Append word space if last char was not a blank if(msgUpper.back() != ' ') { morse += 'w'; } std::cout << morse << std::endl;}int main() { std::string message{"cq cq de ab1cde ab1cde pse k"}; messageToMorse(message);}
Statistics: Posted by beedaddy — Sat Feb 24, 2024 8:29 pm — Replies 5 — Views 91