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

查看:726
本文介绍了警告:返回了局部变量"angles"的地址[-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 float四元数,所以我需要先进行旋转,而要使其进行编译却遇到了巨大的困难.现在它可以编译了,但是我收到了这个警告.

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<float>作为返回类型.

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<float, 3>优于使用std::vectro<float>:

Since the size of the array is fixed at 3, using std::array<float, 3> is better than using std::vectro<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;
}

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

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