'variable' 可能未初始化地用于此函数,有一个可行的解决方法但不明白为什么
'variable' may be used uninitialized in this function, have a working workaround but don't understand why
我搜索并发现了很多类似的问题(答案无疑是好的),但我还没有找到一个我完全理解的问题。我找到了一个可行的解决方案,我真的只是想了解我在第一个示例中做错了什么......
我编写了一个函数,该函数根据声明的原始加速度计值计算 pitch/roll:
uint8_t calcPitchRoll (imu_t * imu, float * pitch, float * roll);
调用函数看起来像(参考行号):
518 float * pRollValue, * pPitchValue; // prepare pointers to floats to store results
519 *pRollValue = 0; // initialize variables
520 *pPitchValue = 0; // initialize variables
521 calcPitchRoll (imu, pPitchValue, pRollValue);
但是,这会导致编译器警告:
main.c:519:25: warning: 'pRollValue' may be used uninitialized in this function
main.c:521:26: warning: 'pPitchValue' may be used uninitialized in this function
但是以下内容确实有效:
float PitchValue, RollValue = 0;
float * pRollValue = &RollValue;
float * pPitchValue = &PitchValue;
calcPitchRoll (imu, pPitchValue, pRollValue);
对我来说,这两个示例在调用 calcPitchRoll 函数时似乎具有相同的 "state",但编译器不同意。
我(认为我)理解的是 *pRollValue = 0
将该值写入变量,所以我认为此时变量已经 space 分配了一个值。是不是理解错了?
你的两个代码示例有很大的不同。
看看这个:
518 float * pRollValue, * pPitchValue; // Here pRollValue is uninitialized
519 *pRollValue = 0; // Here you dereference pRollValue (due to the *)
^
| // So you dereference an uninitialized pointer
|
Dereference of uninitialized pointer
这里
float PitchValue, RollValue = 0;
float * pRollValue = &RollValue; // You define a pointer and at the
// same time you initializes it to
// point to a float
所以这两个代码部分是完全不同的
所以你需要理解指向类型T对象的指针和类型T对象的区别。
代码类似
float * pF;
会给你内存来保存一个指向浮点数的指针但是没有地方可以存储浮点数本身。您需要为指针赋值,使其指向一个浮点数。
所以你需要这样的东西:
float * pF;
float myFloatVariable;
pF = &myFloatVariable; // Now pF points to myFloatVariable
*pF = 42; // These two statements both sets
myFloatVariable = 42; // the variable myFloatVariable to 42
另一种方法是动态分配 - 例如:
float * pF = malloc(sizeof *pF);
assert(pF != NULL);
*pF = 42; // This sets the dynamic allocated float to 42
我搜索并发现了很多类似的问题(答案无疑是好的),但我还没有找到一个我完全理解的问题。我找到了一个可行的解决方案,我真的只是想了解我在第一个示例中做错了什么......
我编写了一个函数,该函数根据声明的原始加速度计值计算 pitch/roll:
uint8_t calcPitchRoll (imu_t * imu, float * pitch, float * roll);
调用函数看起来像(参考行号):
518 float * pRollValue, * pPitchValue; // prepare pointers to floats to store results
519 *pRollValue = 0; // initialize variables
520 *pPitchValue = 0; // initialize variables
521 calcPitchRoll (imu, pPitchValue, pRollValue);
但是,这会导致编译器警告:
main.c:519:25: warning: 'pRollValue' may be used uninitialized in this function
main.c:521:26: warning: 'pPitchValue' may be used uninitialized in this function
但是以下内容确实有效:
float PitchValue, RollValue = 0;
float * pRollValue = &RollValue;
float * pPitchValue = &PitchValue;
calcPitchRoll (imu, pPitchValue, pRollValue);
对我来说,这两个示例在调用 calcPitchRoll 函数时似乎具有相同的 "state",但编译器不同意。
我(认为我)理解的是 *pRollValue = 0
将该值写入变量,所以我认为此时变量已经 space 分配了一个值。是不是理解错了?
你的两个代码示例有很大的不同。
看看这个:
518 float * pRollValue, * pPitchValue; // Here pRollValue is uninitialized
519 *pRollValue = 0; // Here you dereference pRollValue (due to the *)
^
| // So you dereference an uninitialized pointer
|
Dereference of uninitialized pointer
这里
float PitchValue, RollValue = 0;
float * pRollValue = &RollValue; // You define a pointer and at the
// same time you initializes it to
// point to a float
所以这两个代码部分是完全不同的
所以你需要理解指向类型T对象的指针和类型T对象的区别。
代码类似
float * pF;
会给你内存来保存一个指向浮点数的指针但是没有地方可以存储浮点数本身。您需要为指针赋值,使其指向一个浮点数。
所以你需要这样的东西:
float * pF;
float myFloatVariable;
pF = &myFloatVariable; // Now pF points to myFloatVariable
*pF = 42; // These two statements both sets
myFloatVariable = 42; // the variable myFloatVariable to 42
另一种方法是动态分配 - 例如:
float * pF = malloc(sizeof *pF);
assert(pF != NULL);
*pF = 42; // This sets the dynamic allocated float to 42