Linux 警告:函数返回局部变量的地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18866000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
warning: function returns address of local variable
提问by pistal
I am writing a function in C to do some calculations. and I want to return that as an array value to another function this way.
我正在用 C 编写一个函数来做一些计算。我想以这种方式将它作为数组值返回给另一个函数。
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.
我从根本上错过了什么吗?请给我一些提示/解决方案。
采纳答案by StuartLC
double calculated[8]
Allocates memory on the stack, which will be unwound when the function returnsand 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. To return your heap allocated calculation of 8 doubles:
我不确定返回 int 的目的是什么。要返回堆分配的 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*
返回。
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 的评论,分配的数组的所有权从“计算”转移到调用函数,一旦不再需要它就必须释放它。
回答by Sankalp
Firstly, your function's return type is incorrect. It should probably be a pointer to a double
.
首先,您函数的返回类型不正确。它应该是一个pointer 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);
回答by user1969104
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.
其中calculated_val 被假定为双数组。
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;
}