使用 .substring 方法时出现 ESP32 Guru 冥想错误
ESP32 Guru Meditation Error while using .substring method
所以我使用 Arduino IDE 为我的 ESP32 编写了一个函数,我需要编写一个函数来获取十六进制值的组合字符串,如下所示:
ffffff0000ffff0000
然后拆分成一个数组,像这样:
{"ffffff","0000ff","ff0000"}
到目前为止,我已经写了这个:
String colorIndex[] = {};
void processColors(String input){
int count = 0;
for(int i = 0; i < input.length(); i += 6){
colorIndex[count] = input.substring(i,i+6);
count++;
};
for(int i = 0; i < sizeof(colorIndex); i++){
Serial.println(colorIndex[i]);
}
}
但是,我 运行 遇到一个问题,每当此函数为 运行 时,串口都会打印出此错误:
Guru Meditation Error: Core 1 panic'ed (StoreProhibited). Exception was unhandled.
Core 1 register dump:
PC : 0x4000c3f5 PS : 0x00060830 A0 : 0x800d9575 A1 : 0x3ffd2eb0
A2 : 0x00000050 A3 : 0x3ffd2f10 A4 : 0x00000007 A5 : 0x0000ff00
A6 : 0x00ff0000 A7 : 0xff000000 A8 : 0x00000000 A9 : 0x00000050
A10 : 0x00000066 A11 : 0x3ffd3696 A12 : 0x00000007 A13 : 0xed62d3d8
A14 : 0x06000000 A15 : 0x06000000 SAR : 0x00000010 EXCCAUSE: 0x0000001d
EXCVADDR: 0x00000050 LBEG : 0x400014fd LEND : 0x4000150d LCOUNT : 0xfffffffe
Backtrace: 0x4000c3f5:0x3ffd2eb0 0x400d9572:0x3ffd2ed0 0x400d967a:0x3ffd2ef0 0x400d1283:0x3ffd2f10 0x400d1322:0x3ffd2f40 0x400d7345:0x3ffd2f80 0x400d51e9:0x3ffd2fc0 0x400d5279:0x3ffd3000 0x400d8369:0x3ffd3020 0x400d83e9:0x3ffd3060 0x400d89fa:0x3ffd3080 0x40088b7d:0x3ffd30b0
Rebooting...
google了一下,发现这个错误是非法访问内存引起的。是什么原因造成的,我该如何解决?或者,有没有更好的方法来完成我想做的任务?
您将 colorIndex
定义为 String
的空数组。
String colorIndex[] = {};
成为一个空数组,然后你开始往里面存东西……但是你还没有预留任何地方来存东西。因此,当您尝试在循环中保存 colorIndex
子字符串时,它会随机写入某处并导致您看到的异常。
您只需像这样消除分配即可轻松验证这一点:
for(int i = 0; i < input.length(); i += 6){
Serial.println(input.substring(i,i+6));
};
您应该会看到提取的子字符串,而不会出现错误。
您需要声明 colorIndex
以获得足够的 space 来存储您要在其中存储的许多项目。例如,如果你知道你永远不会有超过 3 个字符串,你会写:
String colorIndex[3] = {};
为了防止您的代码意外溢出数组,您应该真正测试循环中的索引。在这种情况下,您的代码应该更像:
#define MAX_COLOR_INDICES 3
String colorIndex[MAX_COLOR_INDICES] = {};
void processColors(String input){
int count = 0;
for(int i = 0; i < input.length(); i += 6){
if(count == MAX_COLOR_INDICES) {
Serial.printf("ran out of room for color indices at %d\n", count);
break;
}
colorIndex[count] = input.substring(i,i+6);
count++;
};
所以我使用 Arduino IDE 为我的 ESP32 编写了一个函数,我需要编写一个函数来获取十六进制值的组合字符串,如下所示:
ffffff0000ffff0000
然后拆分成一个数组,像这样:
{"ffffff","0000ff","ff0000"}
到目前为止,我已经写了这个:
String colorIndex[] = {};
void processColors(String input){
int count = 0;
for(int i = 0; i < input.length(); i += 6){
colorIndex[count] = input.substring(i,i+6);
count++;
};
for(int i = 0; i < sizeof(colorIndex); i++){
Serial.println(colorIndex[i]);
}
}
但是,我 运行 遇到一个问题,每当此函数为 运行 时,串口都会打印出此错误:
Guru Meditation Error: Core 1 panic'ed (StoreProhibited). Exception was unhandled.
Core 1 register dump:
PC : 0x4000c3f5 PS : 0x00060830 A0 : 0x800d9575 A1 : 0x3ffd2eb0
A2 : 0x00000050 A3 : 0x3ffd2f10 A4 : 0x00000007 A5 : 0x0000ff00
A6 : 0x00ff0000 A7 : 0xff000000 A8 : 0x00000000 A9 : 0x00000050
A10 : 0x00000066 A11 : 0x3ffd3696 A12 : 0x00000007 A13 : 0xed62d3d8
A14 : 0x06000000 A15 : 0x06000000 SAR : 0x00000010 EXCCAUSE: 0x0000001d
EXCVADDR: 0x00000050 LBEG : 0x400014fd LEND : 0x4000150d LCOUNT : 0xfffffffe
Backtrace: 0x4000c3f5:0x3ffd2eb0 0x400d9572:0x3ffd2ed0 0x400d967a:0x3ffd2ef0 0x400d1283:0x3ffd2f10 0x400d1322:0x3ffd2f40 0x400d7345:0x3ffd2f80 0x400d51e9:0x3ffd2fc0 0x400d5279:0x3ffd3000 0x400d8369:0x3ffd3020 0x400d83e9:0x3ffd3060 0x400d89fa:0x3ffd3080 0x40088b7d:0x3ffd30b0
Rebooting...
google了一下,发现这个错误是非法访问内存引起的。是什么原因造成的,我该如何解决?或者,有没有更好的方法来完成我想做的任务?
您将 colorIndex
定义为 String
的空数组。
String colorIndex[] = {};
成为一个空数组,然后你开始往里面存东西……但是你还没有预留任何地方来存东西。因此,当您尝试在循环中保存 colorIndex
子字符串时,它会随机写入某处并导致您看到的异常。
您只需像这样消除分配即可轻松验证这一点:
for(int i = 0; i < input.length(); i += 6){
Serial.println(input.substring(i,i+6));
};
您应该会看到提取的子字符串,而不会出现错误。
您需要声明 colorIndex
以获得足够的 space 来存储您要在其中存储的许多项目。例如,如果你知道你永远不会有超过 3 个字符串,你会写:
String colorIndex[3] = {};
为了防止您的代码意外溢出数组,您应该真正测试循环中的索引。在这种情况下,您的代码应该更像:
#define MAX_COLOR_INDICES 3
String colorIndex[MAX_COLOR_INDICES] = {};
void processColors(String input){
int count = 0;
for(int i = 0; i < input.length(); i += 6){
if(count == MAX_COLOR_INDICES) {
Serial.printf("ran out of room for color indices at %d\n", count);
break;
}
colorIndex[count] = input.substring(i,i+6);
count++;
};