Go Down

Topic: Pointer Problems (Read 264 times) previous topic - next topic

misteral

May 16, 2013, 02:10 am Last Edit: May 16, 2013, 02:23 am by misteral Reason: 1
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 :(

Code: [Select]

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: [Select]

Menu
Menu
Menu
(etc)


Actual:
Code: [Select]

(
úóï}ïïßúî¡ìï_[·÷¨?Ñçºßßí?÷÷¶û÷?½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

Arrch


I can still get the issues with this code.


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

misteral


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: [Select]

(
úóï}ïïßúî¡ìï_[·÷¨?Ñçºßßí?÷÷¶û÷?½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

crimony

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.

misteral

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!!)

Go Up