我在编译时遇到这个错误,并在此处检查了其他问题,但没有进一步的进展:
funciones.c: In function ‘Lyapunov’: ../funciones.c:55:2: warning: function returns address of local variable [-Wreturn-local-addr]
return 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++;
}
我收到警告的 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;
}
我假设一定有某种“malloc”,但我试图修复它的尝试是一场灾难。 先感谢您。感谢您的帮助。
最佳答案
您可以按照建议使用 malloc,但是查看您的代码更好的想法是将单个静态缓冲区传递给函数以获取结果(因为您只使用一次缓冲区,然后丢弃它的数据),这样函数签名将是:
void Lyapunov(int ai, int bi, unsigned char rgb[]);
然后在使用该函数之前,您需要定义缓冲区:
unsigned char rgb[3];
然后在你的循环中使用它
Lyapunov(col,linea, rgb);
这样您就不必担心任何内存泄漏(由函数引起)。
关于c - 警告 : function returns address of local variable [-Wreturn-local-addr],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28679558/