{"action":"created","issue":{"url":"https://api.github.com/repos/sebnil/DueFlashStorage/issues/2","labels_url":"https://api.github.com/repos/sebnil/DueFlashStorage/issues/2/labels{/name}","comments_url":"https://api.github.com/repos/sebnil/DueFlashStorage/issues/2/comments","events_url":"https://api.github.com/repos/sebnil/DueFlashStorage/issues/2/events","html_url":"https://github.com/sebnil/DueFlashStorage/issues/2","id":72036373,"number":2,"title":"Writing a pointer returned by a function to (char*) struct member causes junk value after resetting the board.","user":{"login":"avarachanvc","id":4372742,"avatar_url":"https://avatars.githubusercontent.com/u/4372742?v=3","gravatar_id":"","url":"https://api.github.com/users/avarachanvc","html_url":"https://github.com/avarachanvc","followers_url":"https://api.github.com/users/avarachanvc/followers","following_url":"https://api.github.com/users/avarachanvc/following{/other_user}","gists_url":"https://api.github.com/users/avarachanvc/gists{/gist_id}","starred_url":"https://api.github.com/users/avarachanvc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/avarachanvc/subscriptions","organizations_url":"https://api.github.com/users/avarachanvc/orgs","repos_url":"https://api.github.com/users/avarachanvc/repos","events_url":"https://api.github.com/users/avarachanvc/events{/privacy}","received_events_url":"https://api.github.com/users/avarachanvc/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2015-04-30T01:50:04Z","updated_at":"2015-05-05T18:02:23Z","closed_at":null,"body":"```\r\n//void setup(){\r\n//Same as in original example script.\r\n}\r\nvoid loop(){\r\n  /* read configuration struct from flash */\r\n  byte* b = dueFlashStorage.readAddress(4); // byte array which is read from flash at adress 4\r\n  Configuration configurationFromFlash; // create a temporary struct\r\n  memcpy(&configurationFromFlash, b, sizeof(Configuration)); // copy byte array to temporary struct\r\n\r\n\r\nchar string[]=\"Name=Marcus&greeting=goodmorning\";\r\nchar* Name=parsePostData(\"Name\",string); // See function below.\r\n\r\n\r\nSerial.print(\" message:\");\r\n  Serial.print(configurationFromFlash.message);\r\n\r\nconfigurationFromFlash.message = Name;//This is causing problems in Case 1 inside below function.\r\n\r\n// write configuration struct to flash at adress 4\r\n  byte b2[sizeof(Configuration)]; // create byte array to store the struct\r\n  memcpy(b2, &configurationFromFlash, sizeof(Configuration)); // copy the struct to the byte array\r\n  dueFlashStorage.write(4, b2, sizeof(Configuration)); // write byte array to flash\r\n\r\n}\r\n\r\n\r\n\r\nchar* parsePostData(char s[],char t[])\r\n{\r\n  char *pch;\r\n  char *pp=\"Marcus\";\r\n\r\n  char tCpy[512];//Make a copy. Otherwise, strtok works on the char pointer, and original char array gets modified/ corrupted.\r\n  strcpy(tCpy,t);\r\n  pch = strtok (tCpy,\"=&\");\r\n  while (pch != NULL)\r\n  {\r\n      if(strcmp(pch,s)==0) {\r\n            pch= strtok (NULL, \"&\"); \r\n            //**Case 1. what I need. but it is causing issues \r\n            //after I write to flash, and restart the board.**\r\n            //return pch;   \r\n           return stdup(pch);\r\n\r\n            //Case 2. Forced test case. works perfect.   Even after board is reset, values are correct.       \r\n            //return pp;\r\n\r\n\r\n      }else{\r\n        pch = strtok (NULL, \"=&\");  \r\n      }\r\n  }\r\n\r\n}```\r\n"},"comment":{"url":"https://api.github.com/repos/sebnil/DueFlashStorage/issues/comments/99159849","html_url":"https://github.com/sebnil/DueFlashStorage/issues/2#issuecomment-99159849","issue_url":"https://api.github.com/repos/sebnil/DueFlashStorage/issues/2","id":99159849,"user":{"login":"sebnil","id":1711012,"avatar_url":"https://avatars.githubusercontent.com/u/1711012?v=3","gravatar_id":"","url":"https://api.github.com/users/sebnil","html_url":"https://github.com/sebnil","followers_url":"https://api.github.com/users/sebnil/followers","following_url":"https://api.github.com/users/sebnil/following{/other_user}","gists_url":"https://api.github.com/users/sebnil/gists{/gist_id}","starred_url":"https://api.github.com/users/sebnil/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sebnil/subscriptions","organizations_url":"https://api.github.com/users/sebnil/orgs","repos_url":"https://api.github.com/users/sebnil/repos","events_url":"https://api.github.com/users/sebnil/events{/privacy}","received_events_url":"https://api.github.com/users/sebnil/received_events","type":"User","site_admin":false},"created_at":"2015-05-05T18:02:23Z","updated_at":"2015-05-05T18:02:23Z","body":"The reason it does not work is probably because the pointer points to a string in RAM which is volatile during reset. Store the whole string in flash and not a pointer to it, and your code will work. "}}