Global vs Local variables?

Hi all...

I am trying to change the ssid of my ESP32. I have 'ssid' declared as a global variable. In a subroutine I have a local variable 'test' that builds an id via the serial port at boot-up. When 'test' has the correct data, I assign it to 'ssid' and can print BOTH variables to the screen and they are correct. NOW...when I exit THAT routine and try to immediately print 'ssid' again, I get garbage ASCII data. 'ssid' is a GLOBAL variable...why does it "blow up" after exiting the subroutine and only correct withing the subroutine???

need to see your code

The problem is in the code you didn't share

My guess is that the buffer ('test') you're using to build the id is a local variable. Once you exit the function, it goes out of scope, and if the global 'ssid' pointer still points to that local buffer, you're in trouble, because it doesn't exist anymore.

I'd suggest making 'ssid' an std::string or a String, these classes manage their own buffer. Alternatively, make the buffer a static local variable.

Pieter

My guess is that your global variable is a character pointer. If you set the pointer to point to a local variable and that local variable goes out of scope, that part of memory is free to be overwritten.

Change 'ssid' to a character array of sufficient length and initialize it with the initial value. Copy the local variable to the global array with strcpy(dest, src). Now the global array will retain that value until you write over it.