简体   繁体   English

C程序内存违反依赖于std :: cout(?)

[英]C program memory violation dependant on std::cout (?)

I've written an extension in C++ for Python, and I'm currently debugging it. 我已经用Python编写了C ++扩展,我正在调试它。 The extension takes 3 numpy matrices and produces 2 as a result. 扩展需要3个numpy矩阵,结果产生2个。 To the inner C++ function that does the actualy calculation I pass 3 float C arrays (just flattened and converted from input numpy arrays), and return a C float array of arrays. 对于执行实际计算的内部C ++函数,我传递了3个float C数组(刚刚平坦化并从输入numpy数组转换),并返回一个C float数组。 Everything works as intended but ONLY if I print this output array of arrays before returning it. 一切都按预期工作,但只有在我返回它之前打印这个数组的输出数组。

What the hell is going on in here? 这到底是怎么回事?

float** gradient(float* inputs, float* kernels, float* grads, npy_intp* input_dims, npy_intp* kernels_dims, npy_intp* output_dims){


float* g_inputs = new float[batch*h*w*ch_in];
for (int i = 0; i < batch*h*w*ch_in; i++) g_inputs[i] = 0;
float* g_kernels = new float[size*ch_out];
for (int i = 0; i < size*ch_out; i++) g_kernels[i] = 0;


float* ret[2] = {{g_inputs}, {g_kernels}};
std::cout<<ret<<std::endl; //<---without this it doesn't work
return ret;
}

I've omitted irrelevant code for clarity. 为清楚起见,我省略了不相关的代码。

You are returning a pointer to an object with automatic lifetime. 您正在返回一个指向具有自动生命周期的对象的指针。 In other words, your function returns a dangling pointer, which is Undefined Behaviour. 换句话说,您的函数返回一个悬空指针,即未定义行为。

Although aerostatic lizards are an uncommon result of UB, anything can happen and the symptom you observe, unlike the lizards, is common. 虽然空气静力蜥蜴是UB不常见的结果,但是任何事情都可能发生,而且与蜥蜴不同,你观察到的症状很常见。

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

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