【发布时间】:2015-04-25 02:49:14
【问题描述】:
我在编译时收到此错误,并在此处检查了其他问题,但没有进一步的进展:
funciones.c:在函数“Lyapunov”中:../funciones.c:55:2:警告: 函数返回局部变量的地址 [-Wreturn-local-addr]
返回 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”,但我试图修复它的尝试是一场灾难。 先感谢您。任何帮助表示赞赏。
【问题讨论】:
-
重复数百次。
-
您正在尝试返回
rgb,这是一个本地数组,因此在您返回时将超出范围。 -
我不会返回数组指针,而是将 3 个字节的 RGB 打包到单个
int中并返回它,而不会影响动态分配。
标签: c