i would like to use strucs in plain C to manage Data on a microcontroller. I read a lot of online tutorials, but could not find the solution to my problem. The µC always runs into HardFault .
This is the struct definition in the header file:
typedef struct{
uint8_t laenge;
uint8_t rfu;
uint16_t nsi;
uint8_t epc_data[24];
uint16_t crc16;
}epc_memory;
this is the function prototype :
epc_memory decodeACK(uint32_t rxBuffer[]);
The function :
epc_memory decodeACK(uint32_t rxBuffer[])
{
counter=0;
epc_memory mem;
//Anfang finden
while(rxBuffer[counter] > ERROR_) counter++;
makeString(rxBuffer,counter);
startEnding();
counter = decoder();
if(counter > 100){
for (counter=0; counter<4; counter++)
{
mem.laenge = (mem.laenge << 1) | decoded[counter];
}
for (counter=4;counter<6; counter++)
{
mem.rfu = (mem.rfu << 1) | decoded[counter];
}
for (counter=6;counter<15; counter++)
{
mem.nsi = (mem.nsi << 1) | decoded[counter];
}
for(counter=15;counter<(15+mem.laenge);counter++)
{
mem.epc_data[(counter-15)/4] = (mem.epc_data[(counter-15/4)] << 1) | decoded[counter];
}
for(counter=(15+mem.laenge);counter<(31+mem.laenge);counter++)
{
mem.crc16 = (mem.crc16 << 1) | decoded[counter];
}
}
return mem;
}
And this is how it is called in the main :
epc_memory tmp = decodeACK(&rxBuffer);
Can you spot any errors ?
Thanks
edit: just for reference... THIS function works just fine :
uint16_t decodeRN16(uint32_t rxBuffer[])
{
counter=0;
uint16_t rn16 = 0;
//Anfang finden
while(rxBuffer[counter] > ERROR_) counter++;
makeString(rxBuffer,counter);
startEnding();
counter = decoder();
if(counter > 16){
for (counter=0; counter<16; counter++)
{
rn16 = (rn16 << 1) | decoded[counter];
}
}
return rn16;
}
The code as shown misses to initialise mem
before reading it's members' values. This might very well cause undefined behaviour. One outcome then could be a "hard fault".
To fix this replace
epc_memory mem;
by
epc_memory mem = {0};
This latter statement intialises all mem
's members to 0
.
As Jean-François Fabre deleted his answer I want additionally point out that
epc_memory decodeACK(uint32_t rxBuffer[])
if called
decodeACK(&rxBuffer);
is called wrongly.
Assuming the array's defintion to be
uint32_t rxBuffer[1024];
call the function
decodeACK(rxBuffer);
Background
epc_memory decodeACK(uint32_t rxBuffer[])
is the same as
epc_memory decodeACK(uint32_t * rxBuffer)
and passing an array to a function makes it decay to the address of its 1st element. Here this will be uint32_t*
.
Passing &rxBuffer
would evaluate to pass a pointer to an array of 1024 uint32_t
, which would be a uint32_t(3)[1024]
, which clearly is not what decodeACK()
expects.
The reason it still might seem to work sometimes, is that the address of an array in fact is the same address as the address of it's 1st element.
Still passing the array's address here is passing the wrong type, is invoking the infamous Undefined Behaviour, is unreliable code, is wrong code, which the compiler clearly should have pointed you to by issuing a related warning.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.