I came across some code similar to the following:
uint8_t* dataPtr = {0};
switch(index)
{
case 0:
dataPtr = (uint8_t[32])
{
0x33, 0x32, 0x31, 0x30, 0x29, 0x27, 0x38, 0x89,
0x33, 0x32, 0x31, 0x30, 0x29, 0x27, 0x38, 0x89,
0x33, 0x32, 0x31, 0x30, 0x29, 0x27, 0x38, 0x89,
0x33, 0x32, 0x31, 0x30, 0x29, 0x27, 0x38, 0x89,
}
break;
case 1:
dataPtr = (uint8_t[32])
{
0x43, 0x42, 0x41, 0x30, 0x29, 0x27, 0x38, 0x89,
0x33, 0x22, 0x21, 0x30, 0x29, 0x27, 0x38, 0x89,
0x33, 0x12, 0x31, 0x30, 0xAF, 0x27, 0x38, 0x89,
0x33, 0x32, 0x55, 0x30, 0xFF, 0xFF, 0x38, 0x89,
}
break;
and so on...
The code as it's written seems to work. I do not understand how memory is not getting corrupted. The initial definition of dataPtr defines it as a pointer to an empty array of uint8_t with an unknown size. Then, it is casted to a static array of 32 uint8_t's and assigned data.
During the initial definition, how does the compiler know how much memory to allocate for this variable? Is this memory allocated on the stack during runtime? Somewhere else during compile time?
uint8_t* dataPtr = {0};should beuint8_t* dataPtr = NULL;.