简体   繁体   English

警告:函数返回本地变量[-Wreturn-local-addr]的地址

[英]warning: function returns address of local variable [-Wreturn-local-addr]

I get this error when compiling and have checked out other questions here with no further progress: 编译时出现此错误,并在此处检查了其他问题,没有进一步的进展:

funciones.c: In function 'Lyapunov': ../funciones.c:55:2: warning: function returns address of local variable [-Wreturn-local-addr] funciones.c:在函数“ Lyapunov”中:../funciones.c:55:2:警告:函数返回局部变量的地址[-Wreturn-local-addr]
return rgb; 返回rgb;

First of all, I call the 'Lyapunov' function here in another .c : *Please note that in this '.c' I have only posted the part of the code where Lyapunov is called and also the declaration of rgb. 首先,我在另一个.c中调用“ Lyapunov”函数:*请注意,在此“ .c”中,我仅张贴了调用Lyapunov的部分代码以及rgb的声明。

unsigned char rgb[3];

while((int)linea>inicial){                  
        for(col=0;col<asize;col++){             
               rgb = Lyapunov(col,linea);

               fwrite(rgb, 3, image);
        }
        linea++;
}

and the Lyapunov function from where I get the warning is here: 我收到警告的Lyapunov函数在这里:

#include "lyapunov.h"
#include <math.h>


#define CLAMP(x) (((x) > 255) ? 255 : ((x) < 0) ? 0 : (x))


unsigned char *Lyapunov(int ai, int bi){
    int n, m;
    double a, b, lambda, sum_log_deriv, prod_deriv, r, x, rgb_f[3];
    unsigned char rgb[3];   


    double lambda_min = -2.55;
    double lambda_max = 0.3959;

    a = amin + (amax-amin)/asize*(ai+0.5);
    b = bmin + (bmax-bmin)/bsize*(bi+0.5);
    x = 0.5;


        for (m = 0; m < seq_length; m++) {
            r = seq[m] ? b : a;
            x = r*x*(1-x);
        }

    sum_log_deriv = 0;
    for (n = 0; n < nmax; n++) {
        prod_deriv = 1;
        for (m = 0; m < seq_length; m++) {
                r = seq[m] ? b : a;

                prod_deriv *= r*(1-2*x); 
                x = r*x*(1-x);
        }
        sum_log_deriv += log(fabs(prod_deriv));
    }
    lambda = sum_log_deriv / (nmax*seq_length);

    if (lambda > 0) {
        rgb_f[2] = lambda/lambda_max;
        rgb_f[0] = rgb_f[1] = 0;
    } else {
        rgb_f[0] = 1 - pow(lambda/lambda_min, 2/3.0);
        rgb_f[1] = 1 - pow(lambda/lambda_min, 1/3.0);
        rgb_f[2] = 0;
    }


    rgb[0] = CLAMP(rgb_f[0]*255);
    rgb[1] = CLAMP(rgb_f[1]*255);
    rgb[2] = CLAMP(rgb_f[2]*255);

    return rgb;
}

I assume there must be some kind of 'malloc' but my attempts trying to fix it have been a disaster. 我认为肯定有某种“ malloc”,但是我试图修复它的尝试是一场灾难。 Thank you in advance. 先感谢您。 Any help is appreciated. 任何帮助表示赞赏。

You can use malloc as suggested, but looking at your code better idea would be to have a single static buffer passed to a function to have it's result (since you are using the buffer just once, and then discarding it's data), such that the function signature will be: 您可以按照建议的方式使用malloc,但是查看您的代码更好的主意是将单个静态缓冲区传递给函数以产生结果(因为您只使用了一次缓冲区,然后丢弃了它的数据),因此功能签名为:

void Lyapunov(int ai, int bi, unsigned char rgb[]);

Then prior the use of the function you will need to define the buffer: 然后,在使用该功能之前,您需要定义缓冲区:

unsigned char rgb[3];

and then use it in your loop 然后在循环中使用它

Lyapunov(col,linea, rgb);

This way you will not need to worry about any memory leaks (resulting from the function). 这样,您就不必担心任何内存泄漏(由函数导致)。

unsigned char rgb[3];

is local to function and the scope of this array is within the function Lyapunov so once you exit the function this memory is no more valid. 对函数而言是本地的,并且此数组的范围在函数Lyapunov因此一旦退出该函数,此内存将不再有效。 So the warning you are getting is the right one which says never return the local variables address which will lead to undefined behavior when used outside its scope. 因此,您得到的警告是正确的警告,它表示永远不要返回局部变量地址,这将在其范围外使用时导致未定义的行为。

Have your memory allocated of heap and then return the pointer as shown below. 为您的内存分配堆,然后返回指针,如下所示。

unsigned char *rgb = malloc(3);

You trying to return an array rgb which stops existing outside of its local scope ( also the scope of the function ), which stops existing when you return from the function. 您试图返回一个数组rgb ,该数组在其本地范围(也就是该函数的范围)之外停止存在,而从该函数返回时该数组不再存在。 Arrays cannot be returned by value. 数组不能按值返回。

You can put the array in a struct: 您可以将数组放入结构中:

struct rgb_hold
{
    char rgb[3] ;
} ;

and return the struct, which can be returned by value. 并返回可以按值返回的结构。

为了避免所有混乱的malloc / free调用等,请在调用方中定义rgb并将地址传递给函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM