I'm pretty new to C++ / Arduino code, please bear with me. Following code currently gives me a Exception (28): epc1=0x40201287 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000.

I suspect that this is because I'm trying to use the pointer to a local variable char parsedString in drawMenu() which is probably destroyed after running renderMenu() and therefor I get a memory related exception(?). Is that correct?

Secondly, if that is true, I'm wondering if I'm just simply trying too hard to avoid global variables, I already got a ton. And creating for every single parsing variable in a function sounds like overkill. What would be the most efficient way to render my menu?

void drawMenu () {
  char* lines;

  for (int i = 0; i < menuMaxLines; i++) {

void renderMenu(char* &output) {

  // Get current menu items
  MenuItem * menuCurrentItems = getMenuItems();

  // Loop out the menu items at the current position (menuPosition), don't exceed the count of menuitems (menuCurrentItemCount) and don't exceed more then the desired lines (menuMaxLines)
  for (int i = menuPosition; i < menuCurrentItemCount && i < menuPosition + menuMaxLines; i++) {
    MenuItem menuItem = menuCurrentItems[i];
    char parsedString[menuMaxLines];

    // Prefix for the menulabel, when focussed
    if (menuFocussedItem == i) {
      strcat(parsedString, "> ");
    else {
      strcat(parsedString, "  ");

    char output[i - menuPosition][menuMaxWidth];
    strcpy(output[i - menuPosition], parsedString); 
  • 2
    In renderMenu() you are trying to create an array with the name output (which would hide the parameter of the function) with a variable size. That cannot work. Arrays created like this need sizes known at compile time. Otherwise you would need to use dynamic allocation (using new). Why aren't you just creating a simple char array inside of drawMenu()? – chrisl Jun 17 '20 at 22:05
  • 2
    Also: because of the parameter of renderMenu() is hidden, the pointer lines is never set to anything valid. Thus printing the elements of it means reading from invalid memory location. There is just no array allocated behind that pointer – chrisl Jun 17 '20 at 22:07
  • @chrisl, gcc can create an array on stack with a variable for size. – Juraj Jun 18 '20 at 5:44
  • Thanks @chrisl, I value your help. I keep forgetting that arrays need to have a fixed size. So your saying that your first comment could be fixed by using char* lines[menuMaxLines] in drawMenu()? And your second question, I first trying to make that array of char arrays in the function but I was unable to return that and therefor went with the out var. Are you able to provide me with a solution to that? – Ambidex Jun 18 '20 at 5:46
  • @juraj, you assumed correctly. It’s a const int – Ambidex Jun 18 '20 at 5:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.