I have a websocket listener that is giving me a std::string of my message payload (I want the bytes) and I wrote a function that pulls each of the bytes out and puts them into 32 bit integer variables, adds that integer to an array of 32 bit integers, and passes back the pointer address:
uint32_t *get32BitInt(WebsocketsMessage message)
{
std::string raw = message.rawData();
uint32_t chunks[message.length() / 4];
int chunkIndex = 0;
for (int i = 0; i < message.length(); i += 4)
{
uint32_t chunk = 0;
for (int j = 0; j < 4; j++)
{
chunk <<= 8;
chunk |= raw.at(i + j);
}
chunks[chunkIndex] = chunk;
chunkIndex++;
}
return chunks;
}
And I know that parsing and array building works because if I add a loop before I exit the overall function call it prints them out just fine.
And in the function that calls the parser I can print out the address of the pointer that was passed back.
But, as soon as I try to dereference anything my microcontroller crashes:
I know that I'm pretty green to pointers and I'm probably misunderstanding something, but I keep looking at docs and examples of passing arrays as pointers and it seems right.
Also, something worth noting is that it doesn't seem to be an issue with the actual number printing, because if I store the first number of the array into a variable and then try to print a line after the assignment the controller crashes after the assignment but before the next serial print:
uint32_t chunks[message.length() / 4];
- I don't think, that you can do that. Array sizes have to be compile time constants. Or you need to use dynamic memory allocation. which can be bad (though on an ESP32 not as bad as on boards like the Uno).