NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Microcontroller Programming » Multidimentional string arrays - stuck
October 15, 2011 by claesz |
Hi, As usual I must have misunderstood some basic C stuff. I was trying to make a basic menu system where you would have a 2D string array called menuTitle, and a 3D string array of menuItems. It would allow you an unlimited number of submenus. However I got stuck and took put some of the code into a test program to see what I had missed. It made me even more confused. The program below "works" (it will basically print "Main menu"). However, if I reinsert the commented lines (except the duplicate declaration of menuItems, all I get is a black LCD. No compilation errors, just all characters black on the LCD when run. Even more confusing is the fact that the same is the case if I change the declaration of menuItem from fixed numbers into the previously declared values numbMenus and numbItems (comment out line 22 and insert line 23). I am feeling like such an idiot because this is basic, basic stuff, and yet I cannot get it to work. Hoping someone can explain what I am missing.
|
---|---|
October 15, 2011 by claesz |
It appears everyone is busy having a life, so I will just keep talking to myself. I just wanted to see if I was completely insane, and therefore wrote the code in C for command line output on my PC. This works fine. It basically gives you a menu system that you can adapt as you wish, with unlimited sub menus. However, whenever I try 3D arrays on the NK it goes berserk, and I cannot figure out why. Even with the multi dimentional arrays, it should not take up more than 1k of memory so that cannot be the problem. This works fine on my PC: (the same thing (adapted for the LCD and with push button input of course) does not work on the ATMega (see above sample))
|
October 15, 2011 by claesz |
Ok, another update. After more testing, this is the conclusion I have reached:
works in my regular C compiler, but gives crazy results on the NK. On NK this will work:
but not "char text[a][b][20];". I am sure there is a perfectly logical explanation for this, but to me in the examples above, char text[a][b][20] and char text[5][10][20] would be the same. Perhaps someone can help me understand why. |
October 17, 2011 by hevans (NerdKits Staff) |
I cannot think of a logical explanation for what you are seeing. If I think of one I will definitely let you know. For now I suggest you use #define instead to lay out the size of your menus.
will do what you are looking for without all the overhead of creating a variable for things that should be static anyway. Humberto |
October 17, 2011 by claesz |
Thanks! That worked without any problems. Cheers, Claesz |
October 17, 2011 by BobaMosfet |
claesz- It looks like the compiler is creating the 3D array on the stack see how it's subtracting 0x3E8 (1000 decimal) from the combination of registers r27 and r26 (possibly the stack pointer), to grow the stack around it:
You would think it would work, unless something is messing up r26/r27, or the stack is getting stepped on. I noticed that if I used defines, they stuffed the values in r24/r25 and there was no issue. I have worked with many assembly languages on many platforms, but have not done much with ATMEGA168 yet, due to time constrains, so my interpretation may not be perfectly correct. I posted the dissassembly so that others could examine and evaluate it. BM |
October 17, 2011 by claesz |
Thanks! It goes whoosh over my head, I am afraid, but hopefully someone can throw some light on the matter. I followed Humberto's advice and used define and there are no problems. I posted the completed code on the forum if anyone is interested. Thanks again! Claesz |
Please log in to post a reply.
Did you know that interrupts can be used to trigger pieces of code when events happen? Learn more...
|