C++ 从函数返回字符数组时崩溃

C++ 从函数返回字符数组时崩溃,c++,arduino,esp32,C++,Arduino,Esp32,我和一个朋友在一个ESP32网络服务器上合作,我们遇到了麻烦。我的朋友几乎已经放弃了,而我的技术不如他 我们有以下函数,它们可以很好地编译,但会导致ESP32崩溃。 经过大量调试后,我确信在尝试返回c void handle_logs_view(String path){ char** list = sdcard_list_files(path); for (int i=0;list[i]!=NULL;i++){ Serial.println(list[i]);

我和一个朋友在一个ESP32网络服务器上合作,我们遇到了麻烦。我的朋友几乎已经放弃了,而我的技术不如他

我们有以下函数,它们可以很好地编译,但会导致ESP32崩溃。 经过大量调试后,我确信在尝试返回
c

void handle_logs_view(String path){
    char** list = sdcard_list_files(path);
    for (int i=0;list[i]!=NULL;i++){
        Serial.println(list[i]);
    }
}

char** sdcard_list_files(String path){
  Serial.println("Listing files for "+path);
  if (path.compareTo("/")){
      char* c[]={"dir1","file1.log","file2.log","file3.log","file4.log","file5.log",NULL};
      return c;
  }
  return NULL;
}
#endif

异常解码器的结果如下所示:

PC: 0x400d1d1b: handle_logs_view(String) at C:\Users\MickW\Downloads\Front_End-11_create_file_explorer\Front_End-11_create_file_explorer\sample_code\WIFITest/WIFITest.ino line 45
EXCVADDR: 0x00000000

Decoding stack results
0x400d1d1b: handle_logs_view(String) at C:\Users\MickW\Downloads\Front_End-11_create_file_explorer\Front_End-11_create_file_explorer\sample_code\WIFITest/WIFITest.ino line 45
0x400d1e7e: std::_Function_handler   >::_M_invoke(const std::_Any_data &) at C:\Users\MickW\AppData\Local\Temp\arduino_build_351256\sketch\WebServer.cpp line 32
0x400d68ff: std::function ::operator()() const at c:\users\mickw\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-97-gc752ad5-5.2.0\xtensa-esp32-elf\include\c++\5.2.0/functional line 2271
0x400d69a5: FunctionRequestHandler::handle(WebServer&, HTTPMethod, String) at C:\Users\MickW\Documents\Arduino\hardware\espressif\esp32\libraries\WebServer\src\detail/RequestHandlersImpl.h line 45
0x400d6a12: WebServer::_handleRequest() at C:\Users\MickW\Documents\Arduino\hardware\espressif\esp32\libraries\WebServer\src\WebServer.cpp line 633
0x400d6b8d: WebServer::handleClient() at C:\Users\MickW\Documents\Arduino\hardware\espressif\esp32\libraries\WebServer\src\WebServer.cpp line 314
0x400d1d6e: loop() at C:\Users\MickW\Downloads\Front_End-11_create_file_explorer\Front_End-11_create_file_explorer\sample_code\WIFITest/WIFITest.ino line 22
0x400d8ad5: loopTask(void*) at C:\Users\MickW\Documents\Arduino\hardware\espressif\esp32\cores\esp32\main.cpp line 23
0x40089552: vPortTaskWrapper at /Users/ficeto/Desktop/ESP32/ESP32/esp-idf-public/components/freertos/port.c line 143

这来自espressif网站:

禁止装载,禁止存储
当应用程序尝试读取或写入无效内存位置时,会发生此CPU异常。写入/读取的地址位于寄存器转储的EXCVADR寄存器中。若该地址为零,则通常意味着应用程序试图取消对空指针的引用


如果您正在返回指向自动(即基于堆栈的)变量的指针,我将非常感谢您的帮助或建议。当
sdcard\u list\u files
返回时,该变量消失,指针“悬空”


一种解决方案是将
c
声明为
static
。另一种方法(在C++中)是返回一个
std::optional>

您正在返回一个指向自动(即基于堆栈的)变量的指针。当
sdcard\u list\u files
返回时,该变量消失,指针“悬空”


一种解决方案是将
c
声明为
static
。另一种方法(在C++中)是返回一个
std::optional>

C
应该声明为
static
,最好是在代码的顶层。在您的示例中,
c
在堆栈上声明,并在
sdcard\u list\u files()
返回时消失。用
static
属性声明
c
可以解决这个问题。

c
应该声明为
static
,最好是在代码的顶层。在您的示例中,
c
在堆栈上声明,并在
sdcard\u list\u files()
返回时消失。用<代码>静态< /COD>属性声明<代码> C <代码>将修复它。

选择一个语言目标:C或C++。选择一个语言目标:C或C++。我测试声明为静态,并且它工作。哪一个是更好的解决方案?这取决于你。如果您主要使用C语言编写代码,那么
static
就可以了。非常感谢:)我测试了声明为static,并且成功了。哪一个是更好的解决方案?这取决于你。如果您主要使用C语言编写代码,那么
static
就可以了。非常感谢:)