#include <stdio.h>
typedef struct {
float a;
float b;
float c;
} test_t;
int fun_test(void *pdata)
{
test_t *temp = (test_t *)pdata;
if(!((temp->a < temp->b) && (temp->b < temp->c))) {
return -1;
}
return 0;
}
int main(void)
{
unsigned char buf[20] = { 0 };
if(fun_test(buf + 2) < 0) {
printf("Failed.\n");
}
return 0;
}
uj5u.com熱心網友回復:
出現故障陳述句為if(!((temp->a < temp->b) && (temp->b < temp->c))),如果單獨使用if(temp->a < 0)則不會出現問題。uj5u.com熱心網友回復:
復制你的代碼運行只出現 Failed.uj5u.com熱心網友回復:
我使用keil編譯,運行在cortex m3上,如果我在fun_test()函式里面定義變數,將buf里的內容拷貝過來,也不會出現故障,比如這樣:
int func(void *pdata)
{
float temp1 = ((test_t *)pdata)->a;
float temp2 = ((test_t *)pdata)->b;
float temp3 = ((test_t *)pdata)->c;
if(!((temp1 < temp2) && (temp2 < temp3))) {
return -1;
}
return 0;
}
uj5u.com熱心網友回復:
猜測: 這個大概率是越界問題,看 sizeof(test_t) 是多少如果按 結構體位元組對齊是8,至少24位元組
uj5u.com熱心網友回復:
#include <stdio.h>
typedef struct {
float a;
float b;
float c;
} test_t;
int fun_test(void *pdata)
{
test_t *temp = (test_t *)pdata;
printf("a = %f, b = %f, c = %f\n", temp->a, temp->b, temp->c);
if(!((temp->a < temp->b) && (temp->b < temp->c))) {
return -1;
}
return 0;
}
int main(void)
{
unsigned char buf[20] = { 0 };
if(fun_test(buf + 2) < 0) {
printf("Failed.\n");
}
return 0;
}
代碼沒什么問題,輸出failed。
其實要看樓主的編譯器是32位還是64位,如果是64位,那么
test_t *temp = (test_t *)pdata;
這句是越界了。因為8 x 3 = 24 > 18
uj5u.com熱心網友回復:
謝謝!
是32位的,現在看不是越界的問題,因為單個成員訪問的時候沒有出現問題
uj5u.com熱心網友回復:
謝謝!
編譯器是32位的
uj5u.com熱心網友回復:
除錯了一下,if(!((temp->a < temp->b) && (temp->b < temp->c))) {
return -1;
buf[2], buf[3],buf[4], =0
a,b,c, 全為 0, 當然回傳 -1 .
uj5u.com熱心網友回復:
Buf 也是 char 型的.uj5u.com熱心網友回復:
可能是因為涉及到浮點運算,然后你的32板子跑不過來。轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/32524.html
標籤:C語言
上一篇:明天考試,望大神解答一下。
