我想使用C ++ 03在每个MPI节点上定义一个常量常量数组。 M_chunk_sizes
定义了要传递给其他节点并且在运行时不会更改的矩阵的大小。
int* define_chunk_sizes( int S, int world) {
int out[world];
double quotient = static_cast<double> (S) / world;
int maj = ceil(quotient);
for (int i =0; i < world - 1; i++)
out[i] = maj;
out[world-1] = maj + (S - maj*world);
return out;
}
int main() {
const int M = 999; // rows
int world_size = 4;
const int* const M_chunk_sizes = define_chunk_sizes(M, world_size);
}
但是我得到一个warning: address of stack memory associated with local variable 'out' returned [-Wreturn-stack-address]
return out;
。
正确的做法是什么?
答案 0 :(得分:1)
funciton local variables(stack varibales) will go out of scope and life once function returns.
you have use dynamic memory management operators, so allocate memory to out
using
new
and relase memory using
delete
once you done with it.