警告:函数返回局部变量的地址[-Wreturn-本地地址] [英] warning: function returns address of local variable [-Wreturn-local-addr]

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

问题描述

编译时我得到这个错误,并没有进一步的进展这里检查了其他问题:

I get this error when compiling and have checked out other questions here with no further progress:

funciones.c:在函数'李雅普诺夫:../funciones.c:55:2:警告:
  函数返回局部变量的[-Wreturn-本地地址]搜索地址
  返回RGB;

funciones.c: In function ‘Lyapunov’: ../funciones.c:55:2: warning: function returns address of local variable [-Wreturn-local-addr]
return rgb;

首先,我在这里所说的李雅普诺夫函数在其他.C:
*请注意,在这个.C我只贴code,其中李亚普诺夫被称为和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.

unsigned char rgb[3];

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

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

和从那里我得到警告的Lyapunov函数是在这里:

and the Lyapunov function from where I get the warning is here:

#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;
}

我认为必须有某种'malloc的',但我的努力试图修复它是一个灾难。
先谢谢你。任何帮助是AP preciated。

I assume there must be some kind of 'malloc' but my attempts trying to fix it have been a disaster. Thank you in advance. Any help is appreciated.

推荐答案

您可以使用malloc的建议,但看着你的code更好的想法是有传递给函数的一个静态缓冲区有它的结果(因为你正在使用的缓冲区只有一次,然后丢弃它的数据),这样,函数签名是:

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:

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).

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

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