OS: Win 10 Pro 64
IDE: 1.8.15 vs. 1.8.19
This worked in 1.8.15
const uint32_t *v = (const uint32_t *)val;
But a few days ago I found out that in 1.8.19 my program crashes. Sounds very low level to me and a bit beyond my pay grade.
What to do?
OS: Win 10 Pro 64
IDE: 1.8.15 vs. 1.8.19
This worked in 1.8.15
const uint32_t *v = (const uint32_t *)val;
But a few days ago I found out that in 1.8.19 my program crashes. Sounds very low level to me and a bit beyond my pay grade.
What to do?
Please post a complete skeytch that illustrates the problem
Note that it does not have to be the full sketch of your project, just one that can be compiled to show the problem
What exactly happens when the sketch "crashes" ?
uint32_t myFunc(const char * val)
{
const uint32_t * v = (const uint32_t *)val;
SerialUSB.println(v[0], HEX); // Programs stops here.
return v;
}
The program stalls and the USB stops too meaning a double click has to be used when uploading code again.
Post the rest of the sketch please
Not great code
Why do you pass a pointer to char when you need a pointer to a 32 bit unsigned int? What’s the parameter you call the function with?
Also you return a pointer whilst you promised the compiler to return an uint32_t
This would make more sense
uint32_t myFunc(const uint32 * val) {
SerialUSB.println(*val, HEX);
return *val;
}
But I suppose you did not share the real code. Don’t post snippets (Snippets R Us!)
Side note: how do you know it crashes right there?
The compiler version of the boards package that you installed with 1.8.19 might differ from the one that you have installed with 1.8.15; or the optimisations used by the boards package might differ. Although the core files might have changed.
As a result, memory layout can differ and this can reveal bugs in your code.
A clear indication of a bug; most of the time in your code. Somewhere in the code your writing memory that does not belong to the variable that you're writing to. E.g. if you have an array int array[10]
and you try to write to the 11th element (using e.g. array[10] = 13
) your are overwriting memory that is not 'yours'.
As far as I know, your board uses native USB. As a result of the bug, you overwrite a variable that is used by the USB stack which either results in the board not reacting on the software reset or (worse case) the board no longer being detected.
Hi all
Thanks for the responses. I have tried over the week to isolate the problem further, but in vain so far.
It is true that myFunc is not complete. However, in the full program to troubleshoot I have added SerialUSB.println after each line of code and the printing is fine until the first print after the
const uint32_t * v = (const uint32_t *)val;
Running the full function all by it self works. So yes it is likely that the pointer handling is dubious and conflicts with some places in the RAM. The full function is part of a hash calculation.
I will have to dig further.
it's not that it is not complete, it's just wrong... for example did you mean return *v;
instead of return v;
or why do you want to pass a const char *
when what you want is is a const uint32_t *
? that makes no sense...
I’d assume that the char* pointer ends up unaligned wrt that needed for a uint32* access, leading to a memory access “hard fault” (crash) on cortex M0.
I don’t know why it would be different with the older compiler.
Implement getLong(char *) that works even in the face of unaligned pointers.
I have a "classic" solution that works - bit shifting, i.e. processing the input value char by char
uint32_t v = (val[3] << 24) + (val[3] << 16) + (val[1] << 8) + val[0];
uint32_t w = (val[7] << 24) + (val[6] << 16) + (val[5] << 8) + val[4];
and so on.
What is your arduino board?
The code above is hardware-dependent and will give you completely different results on different boards.
Arduino Zero/SAMD21G18A/M0+
ok, I guess that SAMD21 is 32bit mcu and so this code will works fine.
But in general this should be writen as
uint32_t v = ((uint32_t)val[3] << 24) + ((uint32_t)val[3] << 16) + (val[1] << 8) + val[0];
Can you explain in simple terms why this is needed? Why do you need to convert an array of characters to 32bit integers? Are you sending numbers as symbols?
I need to calculate a hash and the way I did at first is the way the third party code is made.
I don't understand
If you need to calculate the hash of char array - why do you need to convert chars to ints?
I mixed up and messed up
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.