ESP32 error convert String to Char Array

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_char() {
	int str_len = str_password.length() + 1;				// convert string to chat
	char char_password[str_len];	
	str_password.toCharArray(char_password, str_len);	
	return char_password;
}

I performed the test and verification with this code:

 Serial.print(str_password); Serial.println(" - string value"); 
 Serial.print(password_char()); Serial.println(" - convert string to char aray"); 
 Serial.printf("Char print: %s \n",password_char());  Serial.println(" - char to printf");

Output from serial monitor here.

18:56:19.262 -> pass666666 - string value
18:56:19.262 ->  - convert string to char aray
18:56:19.262 -> Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.
18:56:19.297 -> Core 1 register dump:
18:56:19.297 -> PC      : 0x400014fd  PS      : 0x00060e30  A0      : 0x800e53c4  A1      : 0x3ffb1ab0  
18:56:19.297 -> A2      : 0x00000000  A3      : 0xfffffffc  A4      : 0x000000ff  A5      : 0x0000ff00  
18:56:19.297 -> A6      : 0x00ff0000  A7      : 0xff000000  A8      : 0x00000000  A9      : 0x3ffb1e60  
18:56:19.297 -> A10     : 0x00000001  A11     : 0x00000000  A12     : 0x3ffb8448  A13     : 0x00000000  
18:56:19.332 -> A14     : 0x00000000  A15     : 0x00000000  SAR     : 0x00000018  EXCCAUSE: 0x0000001c  
18:56:19.332 -> EXCVADDR: 0x00000000  LBEG    : 0x400014fd  LEND    : 0x4000150d  LCOUNT  : 0xffffffff  
18:56:19.332 -> 
18:56:19.332 -> ELF file SHA256: 0000000000000000
18:56:19.332 -> 
18:56:19.332 -> Backtrace: 0x400014fd:0x3ffb1ab0 0x400e53c1:0x3ffb1ac0 0x400e918e:0x3ffb1dd0 0x400e91c6:0x3ffb1e60 0x400de8f6:0x3ffb1ea0 0x400d5416:0x3ffb1f40 0x400dfe26:0x3ffb1fb0 0x4008967a:0x3ffb1fd0
18:56:19.367 ->

The crash of the program for Serial.printf is caused by an invalid input.

Is there any reason why ESP32 behaves differently than ESP8266?
Is another applicable method of converting String to Char Array applicable to ESP32?

Thank you.

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.

void TryConnect(String ssid, String password) {  // try to connect to this network
  char namebuf[ssid.length() + 1];
  ssid.toCharArray(namebuf, ssid.length() + 1);
  char passbuf[password.length() + 1];
  password.toCharArray(passbuf, password.length() + 1);
  STAname = ssid;
  STApass = password;
  WiFi.begin(namebuf, passbuf);
  reconnectiontime = millis();
  toreconnect = true;
}

This works !

In the end, I solved this code modification.

char* password_char(){
    if(str_password.length()!=0){
        char *p = const_cast<char*>(str_password.c_str());
        return p;
    }
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.