警告:返回的局部变量“角度"的地址 [-Wreturn-local-addr] [英] warning: address of local variable 'angles' returned [-Wreturn-local-addr]

查看:63
本文介绍了警告:返回的局部变量“角度"的地址 [-Wreturn-local-addr]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的 ODE(开放动态引擎)模拟中返回一个身体对象的浮点 x、y 和 z 角度值.

I'm trying to return float x, y and z angle values for a body object from my ODE (open dynamics engine) simulation.

float* Creature::eulerAngles(const float &q0, const float &q1, const float &q2, const float &q3){

    float angles[3] = {atan2(2 * (q0*q1 + q2*q3), 1 - 2 * (q1*q1 + q2*q2)),
                      asin( 2 * (q0*q2 - q3*q1)),
                      atan2(2 * (q0*q3 + q1*q2), 1 - 2 * (q2*q2 + q3*q3))};
    return angles;
}

因为 dBodyGetQuaternion 返回 4 个 const 浮点四元数,所以我需要然后进行旋转,我在尝试编译它时遇到了巨大的困难.现在它确实编译了,但我收到了这个警告.

Because dBodyGetQuaternion returns 4 const float quaternions I need to then get the rotations and I've had immense difficulty trying to get it to compile. Now it does compile but I'm getting this warning.

谁能给我解释一下为什么以及这意味着什么?

Could anyone explain to me why and what it means please?

推荐答案

float angles[3] = { ... };

定义一个本地数组.

声明

return angles;

返回指向数组第一个元素的指针.

returns a pointer to the first element of the array.

但是,一旦函数返回,数组就会被破坏.因此,返回的指针是一个悬空指针.

However, the array is destructed as soon as the function returns. Hence, the returned pointer is a dangling pointer.

这就是编译器向您发出的警告.如果在调用函数中取消引用返回的指针,则会调用未定义的行为.

That's what the compiler is warning you about. If you dereference the returned pointer in the calling function, you invoke undefined behavior.

为了返回指向在函数返回后仍然有效的数组的指针,需要分配动态内存并返回动态内存.

In order to return a pointer to an array that will remain valid after the function returns, you need to allocate dynamic memory and return the dynamic memory.

float* Creature::eulerAngles(const float &q0, const float &q1,
                             const float &q2, const float &q3)
{
   float* angles = new float[3];
   angles[0] = atan2(2 * (q0*q1 + q2*q3), 1 - 2 * (q1*q1 + q2*q2));
   angles[1] = asin( 2 * (q0*q2 - q3*q1));
   angles[2] = atan2(2 * (q0*q3 + q1*q2), 1 - 2 * (q2*q2 + q3*q3));

   return angles;
}

请记住,如果您执行上述操作,则必须确保在调用函数的返回指针上调用 delete [].

Keep in mind that if you do the above, you'll have to make sure to call delete [] on the returned pointer in the calling function.

为避免手动分配和释放内存的麻烦,您可以使用 std::vector 作为返回类型.

To avoid the hassles of manually allocating and deallocating memory, you can use std::vector<float> as your return type.

std::vector<float> Creature::eulerAngles(const float &q0, const float &q1,
                                         const float &q2, const float &q3)
{
   std::vector<float> angles(3);
   angles[0] = atan2(2 * (q0*q1 + q2*q3), 1 - 2 * (q1*q1 + q2*q2));
   angles[1] = asin( 2 * (q0*q2 - q3*q1));
   angles[2] = atan2(2 * (q0*q3 + q1*q2), 1 - 2 * (q2*q2 + q3*q3));

   return angles;
}

这样,内存管理会自动为您完成.

With this, memory management is done automatically for you.

由于数组的大小固定为 3,使用 std::array 比使用 std::vector 更好:

Since the size of the array is fixed at 3, using std::array<float, 3> is better than using std::vector<float>:

std::array<float, 3> Creature::eulerAngles(const float &q0, const float &q1, const float &q2, const float &q3)
{
   std::array<float, 3> angles;
   angles[0] = atan2(2 * (q0*q1 + q2*q3), 1 - 2 * (q1*q1 + q2*q2));
   angles[1] = asin( 2 * (q0*q2 - q3*q1));
   angles[2] = atan2(2 * (q0*q3 + q1*q2), 1 - 2 * (q2*q2 + q3*q3));

   return angles;
}

这篇关于警告:返回的局部变量“角度"的地址 [-Wreturn-local-addr]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆