简体   繁体   English

warning:函数返回局部变量的地址

[英]warning: function returns address of local variable

I am writing a function in C to do some calculations. 我正在用C编写一个函数来做一些计算。 and I want to return that as an array value to another function this way. 我希望以这种方式将其作为数组值返回给另一个函数。

455                         calculated_val = calculation(value_perf);


358 int calculation(double* dataset){
359
360         double calculated[8] = {};
361         calculated[0] = dataset[7]/dataset[5];
362         calculated[1] = (dataset[0] + dataset[1] + dataset[2] - dataset[3] - dataset[4])/(dataset[5]);
363         calculated[2] = dataset[3]/dataset[5];
364         calculated[3] = dataset[6]/dataset[5];
365         calculated[4] = dataset[8]/dataset[5];
366         calculated[5] = dataset[9]/dataset[10];
367         calculated[6] = dataset[11]/dataset[5];
368         calculated[7] = dataset[12]/dataset[5];
369         return calculated;
370 }

While, I am doing so..I get the following warnings and I don't understand them. 虽然,我这样做..我得到以下警告,我不明白。

369:2: warning: return makes integer from pointer without a cast [enabled by default]
369:2: warning: function returns address of local variable [enabled by default]

Is there something I missed out fundamentally? 从根本上是否有一些我错过的东西? Please give me some hints/solutions. 请给我一些提示/解决方案。

double calculated[8]

Allocates memory on the stack, which will be unwound when the function returns and thus not safe for the calling function to access. 在堆栈上分配内存, 当函数返回时将释放内存,因此调用函数无法安全访问。

Instead, use 相反,使用

double* calculated = malloc(8 * sizeof(double));

to allocate it on the heap, which can then shared across your program. 在堆上分配它,然后可以在您的程序中共享它。

Edit 编辑

I'm not sure what was intended by return an int. 我不确定返回int的意图是什么。 To return your heap allocated calculation of 8 doubles: 要返回堆分配的8个双倍计算:

#include "stdlib.h"
// ...
double* calculation(double* dataset){
    double* calculated = (double*)malloc(8 * sizeof(double));
    calculated[0] = dataset[7]/dataset[5];
    // Other assignments ... 
    return calculated;
}

Note that your calling code needs to be adjusted to accomodate the double* return as well. 请注意,您的调用代码需要调整以适应double* return。

As per Gauthier's comment, ownership of the allocated array transfers from 'calculation' to the calling function, which must release it once it is no longer needed. 根据Gauthier的评论,分配的数组的所有权从“计算”转移到调用函数,调用函数必须在不再需要时释放它。

Firstly, your function's return type is incorrect. 首先,函数的返回类型不正确。 It should probably be a pointer to a double . 它可能应该是pointer to a doublepointer to a double

Secondly, you are returning the address of a local variable which is allocated on the stack and as soon as you return from the function, that variable goes out of the picture and similarly it's address. 其次,您将返回在堆栈上分配的局部变量的地址,并且一旦从函数返回,该变量就会脱离图片,类似于它的地址。

So, if you really want to return the address, then you should use: 所以,如果你真的想要返回地址,那么你应该使用:

double* calculated = malloc(sizeof(double)*8);

You can take an additional parameter where the result is returned. 您可以在返回结果的位置获取其他参数。

void calculation(double* dataset, double * result)

And call the function as below 并调用该函数如下

calculation(value_perf, calculated_val);

where calculated_val is assumed to be declared as double array. 其中calculate_val被假定为声明为double数组。

For convenience of using the returned value in another function in the same expression, you can return the same parameter. 为了方便在同一表达式中的另一个函数中使用返回值,可以返回相同的参数。

double * calculation(double* dataset, double * result)
{
    ...
    return result;
}

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

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