Home > Back-end >  Please have a look the great god, why can appear when this code runs hard fault?
Please have a look the great god, why can appear when this code runs hard fault?

Time:12-09

#include

Typedef struct {
Float a;
Float b;
Float c;
} test_t;

Int fun_test (void * pdata)
{
Test_t * temp=pdata (test_t *);
if(! ((temp & gt; A. & lt; Temp - & gt; B) & amp; & (temp & gt; B & lt; Temp - & gt; C))) {
return -1;
}
return 0;
}

Int main (void)
{
Unsigned char buf [20]={0};
If (fun_test (buf + 2) & lt; 0 {
Printf (" Failed. \ n ");
}
return 0;
}

CodePudding user response:

Malfunction statement for the if (! ((temp & gt; A. & lt; Temp - & gt; B) & amp; & (temp & gt; B & lt; Temp - & gt; C))), if used separately the if (temp - & gt; A. & lt; 0) there is no problem,

CodePudding user response:

Copy your code to run only Failed.

CodePudding user response:

refer to the second floor CHXCHXKKK response:
copy your code to run only Failed.


I use the keil compile and run on the cortex m3, if I were in fun_test () function to define variables, copy the contents of the buf to come over, also won't appear fault, such as this:
Int func (void * pdata)
{
Float temp1=((test_t *) pdata) - & gt; a;
Float temp2=((test_t *) pdata) - & gt; b;
Float temp3=((test_t *) pdata) - & gt; c;

if(! ((temp1 & lt; Temp2) & amp; & (temp2 & lt; Temp3))) {
return -1;
}

return 0;
}

CodePudding user response:

Speculation: the big probability is cross-border issues, see sizeof (test_t) how much is the
If according to the structure byte alignment is eight, at least 24 bytes

CodePudding user response:

 # include & lt; Stdio. H> 

Typedef struct {
Float a;
Float b;
Float c;
} test_t;

Int fun_test (void * pdata)
{
Test_t * temp=pdata (test_t *);
Printf (" a=% f, b=% f, c=% f \ n ", temp - & gt; A, temp - & gt; B, temp & gt; C);
if(! ((temp & gt; A. & lt; Temp - & gt; B) & amp; & (temp & gt; B & lt; Temp - & gt; C))) {
return -1;
}
return 0;
}

Int main (void)
{
Unsigned char buf [20]={0};
If (fun_test (buf + 2) & lt; 0 {
Printf (" Failed. \ n ");
}
return 0;
}

Code will be a problem, the output failed,
Actually depends on the building of the compiler is 32 bit or 64 bit, if is 64,
 test_t * temp=pdata (test_t *); 

This sentence is crossing the line, for 8 x 3=24 & gt; 18

CodePudding user response:

reference 4 floor zgl7903 response:
guess: the big probability is cross-border issues, see sizeof (test_t) how much is the
If according to the structure byte alignment is eight, at least 24 bytes


Thank you very much!
Is 32 bit, now is not the problem of crossing the line, because a single member access without problems

CodePudding user response:

The
reference 5 building self-confidence boy reply:
 # include & lt; Stdio. H> 

Typedef struct {
Float a;
Float b;
Float c;
} test_t;

Int fun_test (void * pdata)
{
Test_t * temp=pdata (test_t *);
Printf (" a=% f, b=% f, c=% f \ n ", temp - & gt; A, temp - & gt; B, temp & gt; C);
if(! ((temp & gt; A. & lt; Temp - & gt; B) & amp; & (temp & gt; B & lt; Temp - & gt; C))) {
return -1;
}
return 0;
}

Int main (void)
{
Unsigned char buf [20]={0};
If (fun_test (buf + 2) & lt; 0 {
Printf (" Failed. \ n ");
}
return 0;
}

Code will be a problem, the output failed,
Actually depends on the building of the compiler is 32 bit or 64 bit, if is 64,
 test_t * temp=pdata (test_t *); 

This sentence is crossing the line, for 8 x 3=24 & gt; 18


Thank you very much!
Is 32-bit compiler

CodePudding user response:

Debugging,
if(! ((temp & gt; A. & lt; Temp - & gt; B) & amp; & (temp & gt; B & lt; Temp - & gt; C))) {
return -1;

Buf buf [2], [3], buf [4],=0
A, b, c, whole is 0, of course, return 1.

CodePudding user response:

Buf is char.

CodePudding user response:

Probably because involves the floating point arithmetic, and then you 32 boards to run to come over,
  • Related