Hello, I need help. The following subroutine performs the conversion from String to Char array. Both the UNO and the ESP8266 were functional. After using for ESP, the translation does not report any error, but the conversion does not work.
char_password is allocated on the stack as a local variable to the char * passwprd_char function, you can return a pointer to it but as soon as you return the memory is available to be used by anything else so is effectively lost.
Is there any reason why ESP32 behaves differently than ESP8266?
It is not performing differently, that method doesn’t work on an ESP8266 either. If you need pass a char* you have to create it within the scope that is calling the function (or globally but that has different issues in size.)
Basically you can’t write a function returning a char*, simply because you return the pointer and then free the stack, unless the pointer to nothing is all you’d want. Hence you can return a String, because it is an object created on the heap. If you put those lines in the function that calls the function that takes the char* as an argument, all is fine.
Deva_Rishi:
It is not performing differently, that method doesn’t work on an ESP8266 either. If you need pass a char* you have to create it within the scope that is calling the function (or globally but that has different issues in size.)
Basically you can’t write a function returning a char*, simply because you return the pointer and then free the stack, unless the pointer to nothing is all you’d want. Hence you can return a String, because it is an object created on the heap. If you put those lines in the function that calls the function that takes the char* as an argument, all is fine.
This subroutine worked until the transfer to ESP32.
Can you show me an example?
borgm:
This subroutine worked until the transfer to ESP32.
No it did not "work". You simply got lucky that the particular stack locations weren't overwritten before you tried to access the returned value. The fact that bad code appears to "work" on one platform is no guarantee that it will appear to "work" on others. You'd have to look at the assembler code produced for each processor to trace the reason why.