Pages: [1]   Go Down
Author Topic: Pointer Problems  (Read 247 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Help Please?! I'm attempting to make a linked list. You press button A it goes to the next node, button B goes back. At its simplest here I have a loop I'm just printing out the value of the current node item. The first time, maybe first two times it prints correctly, then from then on it just prints garbage.

I've simplified my code for easier debugging and readability, I can still get the issues with this code. I'm sure I'm doing one thing absolutely wrong but I just can't figure it out smiley-sad

Code:
typedef struct node {
  char *str;
  struct node *next;
  struct node *prev;
} itms;

itms *cItms;

void setup(){
  Serial.begin(9600);

  itms rootMenu = {"Menu",0, 0  };
  itms oneMenu = {"One", &rootMenu, 0  };
  itms twoMenu = {"Two\0",&oneMenu, &rootMenu  };

  rootMenu.next = &oneMenu;
  rootMenu.prev = &twoMenu;
  oneMenu.next = &twoMenu;

  cItms = &rootMenu;

   delay(500);
  Serial.println(cItms->str);

}

void loop() {
   delay(500);
   Serial.println(cItms->str);
}

edit: adding an example of said garbage.

Expected:
Code:
Menu
Menu
Menu
(etc)

Actual:
Code:
(
úóï}ïïßúî¡ìï_[·÷¨ŸÑçºßßíˆ÷÷¶û÷?½x#úßöüfvñ5ío÷„ó].ÿÝÔ×kÝÕݟýÿÔKþ>ï杜ën{YÙï_îØ>`o~ëûïó'·Å¨Í½?¡Þ›ýüô×óþª¿íoóê§;ÿlöô/þ½‹åþ×ëÚï½ÄÖP_2¯µßks?æ÷»\Ôê|®rÎÞó¾kÒzɳúzßx]]í7÷~w»»·ùFyšÓb~Ò.Gm“éíëE±:ë7þ<wûÕߌ;åCÊÏu×ï^Ûÿÿéï÷ÌΒ¿ïߪïWºÒÜüîõWVæéü½âíÿóû7c}{ùè»|ßz|?÷ÿÃ]¬µ_üßôû›Ùû·ÿ|ÿ»ß{;“ÕÕ×pùú½Ê£¯ßu>ÿѼOýßÞ¼û%¾›ví¯é¿¿uÑûíßݯû§æ¿ÿþøóîù.IóV\=ôÿ•üÝ»ÿó¿ïk\Þüø»+íF«¼ÿjgï[ݟñ~öýó›ûÿ/:{ý¯øßÁúÎü}˜íçý/ïÜuUý{ËÛÚø[ÎÊ1ó®Üßÿ!uÏ¢×½ïOéëç.ïñž\üç{ï_-ýw¾³øÙeÏïU¿µça_Óû7÷ÿëù¯w¿?Å]¿ç¾úC÷ÿŒ¿ö¾~•zÛ§ž¿Àvú»õºí^·ùm:Ÿ§ï|·ó•OÚºÐ:àÏ®Çßw~‡û {zDüïûﳟó»ó)¯ýý€ÿÍìoÿ¹ÿ©•±÷/3qƒûÿ¾–̆n›;~ï®õ[Ùs‰?nù‘
÷¿ûþ½ÁÛeÆ­›÷ÝëŸý^®}wÝþ?K~®Ï÷_ÊÕï¾ßç_¯—ò––ýúw)ÇwŸn/9 ¸xÖ¿_[[µl¥ß÷Í¿¿óï÷nÇýÕõáM[ßßßüüû­Ú?zåóÓûÝõæßÓlλýji‘ªó¬½êùý«y_¼º)ß¾þ}Ñíÿ6½ý}çÞïn¼û.ó¿þmá/ÿ>õýkzíé69ò}ký.uýºÔäôËß:ÜϬÿu­Ï͵Ø{Êÿ‰>¾ûÁ7
Getting this issue on 1.03 and 1.04 using Arduino Uno R3, tried on 2 different machines.

Thanks
« Last Edit: May 15, 2013, 07:23:43 pm by misteral » Logged

California
Offline Offline
Faraday Member
**
Karma: 91
Posts: 3430
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I can still get the issues with this code.

Sorry, but it doesn't work isn't a valid way to describe a problem.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry, but it doesn't work isn't a valid way to describe a problem.


Sorry I thought this covered it

from then on it just prints garbage.

Here's an example of said garbage:

Code:
(
úóï}ïïßúî¡ìï_[·÷¨ŸÑçºßßíˆ÷÷¶û÷?½x#úßöüfvñ5ío÷„ó].ÿÝÔ×kÝÕݟýÿÔKþ>ï杜ën{YÙï_îØ>`o~ëûïó'·Å¨Í½?¡Þ›ýüô×óþª¿íoóê§;ÿlöô/þ½‹åþ×ëÚï½ÄÖP_2¯µßks?æ÷»\Ôê|®rÎÞó¾kÒzɳúzßx]]í7÷~w»»·ùFyšÓb~Ò.Gm“éíëE±:ë7þ<wûÕߌ;åCÊÏu×ï^Ûÿÿéï÷ÌΒ¿ïߪïWºÒÜüîõWVæéü½âíÿóû7c}{ùè»|ßz|?÷ÿÃ]¬µ_üßôû›Ùû·ÿ|ÿ»ß{;“ÕÕ×pùú½Ê£¯ßu>ÿѼOýßÞ¼û%¾›ví¯é¿¿uÑûíßݯû§æ¿ÿþøóîù.IóV\=ôÿ•üÝ»ÿó¿ïk\Þüø»+íF«¼ÿjgï[ݟñ~öýó›ûÿ/:{ý¯øßÁúÎü}˜íçý/ïÜuUý{ËÛÚø[ÎÊ1ó®Üßÿ!uÏ¢×½ïOéëç.ïñž\üç{ï_-ýw¾³øÙeÏïU¿µça_Óû7÷ÿëù¯w¿?Å]¿ç¾úC÷ÿŒ¿ö¾~•zÛ§ž¿Àvú»õºí^·ùm:Ÿ§ï|·ó•OÚºÐ:àÏ®Çßw~‡û {zDüïûﳟó»ó)¯ýý€ÿÍìoÿ¹ÿ©•±÷/3qƒûÿ¾–̆n›;~ï®õ[Ùs‰?nù‘
÷¿ûþ½ÁÛeÆ­›÷ÝëŸý^®}wÝþ?K~®Ï÷_ÊÕï¾ßç_¯—ò––ýúw)ÇwŸn/9 ¸xÖ¿_[[µl¥ß÷Í¿¿óï÷nÇýÕõáM[ßßßüüû­Ú?zåóÓûÝõæßÓlλýji‘ªó¬½êùý«y_¼º)ß¾þ}Ñíÿ6½ý}çÞïn¼û.ó¿þmá/ÿ>õýkzíé69ò}ký.uýºÔäôËß:ÜϬÿu­Ï͵Ø{Êÿ‰>¾ûÁ7
Logged

Australia
Offline Offline
Full Member
***
Karma: 0
Posts: 100
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You are declaring rootMenu, oneMenu and twoMenu in the setup() function, these are being allocated on the stack, and reclaimed when you exit setup. You're keeping the pointer to rootMenu as it is declared global, then when you call Serial.println() it is using the memory previously used by the variables rootMenu, oneMenu and twoMenu, and overwriting the data there.

Declare your variables rootMenu, oneMenu and twoMenu at the global scope.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OMG That's crazy enough to work. I was looking at  it thinking it must be getting junked somewhere but figured since there's pointers between all 3 that it would be kept around (too many years of Java garbage collection!!)
Logged

Pages: [1]   Go Up
Jump to: