我很幸运在这里通过阅读别人的问题找到了很多有用的答案,但是这次我完全无能为力,所以我不得不自己提出一个问题:
我尝试创建一个将卷积应用于数据系列的程序。对于具有不同长度的卷积核(=特定数字的数组)是必需的。
我通过使用 float**
来实现它并在两次取消引用的变量中插入值。数组的个数是固定的,每个数组的长度不是固定的,所以“子数组”是用new
分配的—在函数中 CreateKernels
在 if
之后.
此函数然后返回 float**
连同另一个指针捆绑为 main 的结构。
问题来了:
我用调试 watch 查看了内核指针的取消引用值。一切正常,所有数字都在 CreateKernels
之后符合预期返回(即从 main
范围查看内存)。但是在 main
中的下一个命令之后我的数字完全搞砸了。
如果我尝试在后续代码中使用数据,则会出现段错误。
那么我目前的推理是:
当我使用 new
创建它们应该在堆中的变量,并且应该留在那里直到我 free[]
变量——无论如何它们不应该被限制在CreateKernels
的范围内.将指针分配给内核结构并返回它对你们中的某些人来说可能很奇怪,但它确实有效。
所以真正弄乱我的数据的是 CreatKernels
之后的下一个命令.初始化 int
而不是创建 fstream
不会弄乱我的号码。但是为什么?
这是我的操作系统内存管理出错了吗?或者这是一个愚蠢的编程错误?
我正在运行 Ubuntu 12.04-64 位并同时使用 Code::Blocks
和 g++
用于编译(所有默认设置)并且两个可执行文件都给我一个段错误。
如果有任何关于此问题的提示或经验,我将不胜感激!
这是相关代码:
#include <string>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <iomanip>
#define HW width/2
#define width1 4 // kernel width-1 (without peak)
using namespace std;
struct kernel_S
{
const float ** ppkernel;
const float * pnorm;
};
void BaseKernel(const int & width, float * base) // function that fills up an 1d-array of floats at the location of base
{
for(int i=0; i<=HW-1; i++) // fill left half as triangle
{
base[i] = i+1;
}
base[HW] = HW+1; // add peak value
for(int i=HW+1; i<=width; i++) // fill right half as decreasing triangle
{
base[i] = width-i+1;
}
}
kernel_S CreateKernels(const int &width) // function that creates an array of arrays (of variable length)
{
float base_kernel[width+1]; // create a full width kernel as basis
BaseKernel(width, base_kernel);
float * kernel[width+1]; // array of pointers, at each destination of a pointer a kernels is stored
float norm[width+1]; // norm of kernel
for(int j=0; j<=width; j++) // fill up those individual kernels
{
norm[j] = 0;
if(j<=HW) // left side up to peak
{
kernel[j] = new float[HW+j+1]; // allocate mem to a new array to store a sub-kernel in
for(int i=0; i<=HW+j; i++)
{
*(kernel[j]+i) = base_kernel[HW-j+i]; //use values from base kernel
norm[j] += base_kernel[HW-j+i]; // update norm
}
}
else if(j>=HW+1)
{
kernel[j] = new float[HW+width-j+2];
for(int i=0; i<=HW+width-j; i++)
{
*(kernel[j]+i) = base_kernel[i];
norm[j] += base_kernel[i]; // update norm
}
}
}
kernel_S result; // create the kernel structure to be returned
result.ppkernel = (const float **) kernel; // set the address in the structure to the address of the generated arrays
result.pnorm = (const float *) norm; // do the same for the norm
return result;
}
int main()
{
kernel_S kernels = CreateKernels(width1); // Create struct of pointers to kernel data
ifstream name_list(FILEPATH"name_list.txt", ios::in);// THIS MESSES UP THE KERNEL DATA
// some code that would like to use kernels
return 0;
}
最佳答案
请看
您返回指向本地堆栈数据(内核和规范)的指针。你应该动态分配:
float ** kernel = new float*[width+1]; // array of pointers, at each destination of a pointer a kernels is stored
float *norm = new float[width+1]; // norm of kernel
记得用 delete[] 删除。
但是,我建议改用 std::vector 或 std::array
#include <string>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#define HW width/2
#define width1 4 // kernel width-1 (without peak)
using namespace std;
typedef std::vector<float> floatV;
typedef std::vector<floatV> floatVV;
struct kernel_S
{
floatVV kernel;
floatV norm;
};
floatV BaseKernel(int width) // function that fills up an 1d-array of floats at the location of base
{
floatV base;
base.resize(width + 1);
for(int i=0; i<=HW-1; i++) // fill left half as triangle
{
base[i] = i+1;
}
base[HW] = HW+1; // add peak value
for(int i=HW+1; i<=width; i++) // fill right half as decreasing triangle
{
base[i] = width-i+1;
}
return base;
}
kernel_S CreateKernels(const int &width) // function that creates an array of arrays (of variable length)
{
const floatV base_kernel = BaseKernel(width); // create a full width kernel as basis
kernel_S result; // create the kernel structure to be returned
result.kernel.resize(base_kernel.size());
result.norm.resize(base_kernel.size());
for(int j=0; j<=width; j++) // fill up those individual kernels
{
result.norm[j] = 0;
if(j<=HW) // left side up to peak
{
result.kernel[j].resize(HW+j+1); // allocate mem to a new array to store a sub-kernel in
for(int i=0; i<=HW+j; i++)
{
result.kernel[j][i] = base_kernel[HW-j+i]; // use values from base kernel
result.norm[j] += base_kernel[HW-j+i]; // update norm
}
}
else if(j>=HW+1)
{
result.kernel[j].resize(HW+width-j+2);
for(int i=0; i<=HW+width-j; i++)
{
result.kernel[j][i] = base_kernel[i];
result.norm[j] += base_kernel[i]; // update norm
}
}
}
return result;
}
int main()
{
kernel_S kernels = CreateKernels(width1); // Create struct of pointers to kernel data
ifstream name_list("name_list.txt", ios::in);
// some code that would like to use kernels
return 0;
}
注意 如果您希望kernel
和norm
在结果结构中是const
,只需将整个结构常量:
const kernel_S kernels = CreateKernels(width1);
关于c++ - 堆数据困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13431418/