curious about why an almost equivalent C code can avoid LoadProhibited error

TsXor-V
Posts: 1
Joined: Tue Nov 08, 2022 5:02 pm

curious about why an almost equivalent C code can avoid LoadProhibited error

Postby TsXor-V » Tue Nov 08, 2022 5:04 pm

```c
STATIC mp_int_t getuwidth(mp_int_t cord) {
mp_int_t uw = 0;
switch (chrtype(cord)) {
case 0: uw = 1;
break;
case 1: uw = 1;
break;
case 2: uw = 2;
break;
case 3: uw = 2;
break;
case 4: uw = 2;
break;
}
return uw;
}
```
After being executed for 20+ time, it will cause LoadProhibited error.

```c
STATIC mp_int_t getuwidth(mp_int_t cord) {
if (chrtype((uint32_t)cord) <= 1) {
return 1;
} else {
return 2;
}
}
```
But this have no such problem, why?

cruvus
Posts: 59
Joined: Fri Jul 08, 2022 5:08 pm
Location: Planet Earth

Re: curious about why an almost equivalent C code can avoid LoadProhibited error

Postby cruvus » Thu Nov 10, 2022 7:09 pm

In most cases this is a sign of a buffer overflow or some screwup with an interrupt handler or a similar problem in the rest(!) of your code. Take a detailed look at the core dump.
If you change the source, even if it is equivalent, the resulting assembler code might be longer or shorter or quicker or slower. This can dig up or hide problems that were there all along (and not in the code you changed).
ESP32 / ESP-IDF 5.1.4


Who is online

Users browsing this forum: No registered users and 101 guests