/* An Error example, How NOT to return a SafeString by Matthew Ford Copyright(c)2020 Forward Computing and Control Pty. Ltd. This example code is in the public domain. install the SafeString library from the Arduino Library manager or download and install the SafeString zip file from www.forward.com.au/pfod/ArduinoProgramming/SafeString/index.html */ #include "SafeString.h" int count = 0; void setup() { Serial.begin(9600); for (int i = 10; i > 0; i--) { Serial.print(' '); Serial.print(i); delay(500); } Serial.println(); Serial.println(F("This example tries to pass back a SafeString reference as a return. The compile issues a warning")); Serial.println(F("warning: reference to local variable 'str' returned")); Serial.println(F(" The SafeString str is created on the method stack and is completely discarded when the method returns")); } SafeString& loopCountStr(int num) { createSafeString(str, 40, "loop count = "); // create a SafeString str += num; // update it return str; // try and return it } void loop() { count = 1; Serial.println(loopCountStr(count)); // try and print result Serial.println(F(" Finished")); while (1) { } // stop here }