Home > other >  The arm of the aligned memory access problem
The arm of the aligned memory access problem

Time:09-16

 # pragma pack (push, 1) 
Typedef struct
{
Unsigned short it;
Float f;
} OUTDATA;
# pragma pack (pop)

OUTDATA * p_out;//the current value

Float GetInFloat (int cc)
{
Float f=1.23;
Return the f;
}

Int CalculateF (float * data)
{
* data=https://bbs.csdn.net/topics/GetInFloat (0);//arm in this way will trigger bus error signal SIGBUS, Alignment trap (error accessing unaligned memory space)
return 1;
}
Int main (int arg c, char * argv [])
{
P_out=new OUTDATA;
Memset (p_out, 0, sizeof (OUTDATA));

CalculateF (& amp; P_out - & gt; F);

return 0;
}

The above code has a problem, because the data address divided by 4 in addition to the whole, will be submitted to the following error Alignment trap: not handling instruction edc37a00 at [& lt; 0001061 c & gt;]
Unhandled fault: alignment exception (0 x801) at 0 x0119500a
PGD=8 a7dc000
PGD=8 a6c8831 [0119500 a] * and * pte=8 c5be75f, * ppte=8 c5bec7f
Bus error

But if you can change all the float to an int, but after change int, the address of the data or can exactly divisible by four can succeed,
If you don't change the float to an int, the * data=https://bbs.csdn.net/topics/GetInFloat (0) directly into * data=1.23 is correct, very speechless, for according to

CodePudding user response:

Check the three places:
1, is a 32-bit MCU, or how many? Just call GetInFloat, involves the float into and out of the stack,
2, the compiler options, whether to use address automatically aligned?
3, support for the float, the support is soft or hard, if soft support, the compiler address automatically align options are consistent,

CodePudding user response:

reference 1st floor silent_world response:
check the three places:
1, is a 32-bit MCU, or how many? Just call GetInFloat, involves the float into and out of the stack,
2, the compiler options, whether to use address automatically aligned?
3, support for the float, the support is soft or hard, if soft support, the compiler address automatic alignment options are consistent,

Thank you, I this is ARM v7 32 bit, the compiler support automatic processing byte alignment, support for float soft or hard support I don't know, I put the * data=https://bbs.csdn.net/topics/GetInFloat (0) to float * test; Test=GetInFloat (0); * data=* test; Is this also can turn, for a moment, very depressed, later have time again, starting from the assembly instruction of see why not
  • Related