c++ 中的堆损坏
当我在 Visual C++ 2010 中运行代码时,我在运行时遇到错误。
void dct2(){
float** G = new float*[len];
for(int u = 0; u < len; u++){
G[u] = new float[len];
for(int v = 0; v < len; v++){
G[u][v] = 0;
for(int x = 0; x < len; x++){
for(int y = 0; y < len; y++){
G[u][v] += a(u) * a(v) * (float)mat[x][y] * cos((PI / 8) * u * (x + 0.5)) * cos((PI / 8) * v * (y + 0.5));
}
}
}
}
doublecpy(G);
}
void doublecpy(float** d){
for(int i = 0; i < len; i++){
for(int j = 0; j < len; j++){
if(d[i][j] >= 0)
mat[i][j] = (int)(d[i][j] + 0.5);
else
mat[i][j] = (int)(d[i][j] - 0.5);
}
}
for(int i = 0; i < len; i++)
delete[] d[i];
delete[] d;
}
错误出现在以下行中:delete[] d[i]; 请告诉我这段代码是否有任何问题或任何建议。
I'm getting an error in runtime when I run my code in visual c++ 2010.
void dct2(){
float** G = new float*[len];
for(int u = 0; u < len; u++){
G[u] = new float[len];
for(int v = 0; v < len; v++){
G[u][v] = 0;
for(int x = 0; x < len; x++){
for(int y = 0; y < len; y++){
G[u][v] += a(u) * a(v) * (float)mat[x][y] * cos((PI / 8) * u * (x + 0.5)) * cos((PI / 8) * v * (y + 0.5));
}
}
}
}
doublecpy(G);
}
void doublecpy(float** d){
for(int i = 0; i < len; i++){
for(int j = 0; j < len; j++){
if(d[i][j] >= 0)
mat[i][j] = (int)(d[i][j] + 0.5);
else
mat[i][j] = (int)(d[i][j] - 0.5);
}
}
for(int i = 0; i < len; i++)
delete[] d[i];
delete[] d;
}
the error comes in the line: delete[] d[i];
please tell me if there's anything wrong with this piece of code or any advice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
除了你不应该以这种方式编写 C++ 的事实之外(这只是带有 new 而不是 malloc 的 C),我看不到任何内存错误,但不知道 mat 是什么以及它是如何分配的。
Apart from the fact you should not write C++ this way (this is just C with new instead of malloc) I can't see any memory errors but do not know what mat is and how it was allocated.
我修改了您的离散余弦变换和蝴蝶代码以适合我的 C 编译器(不是 C++)。我为那些我没有的虚拟函数和常量插入了。它可以工作(我测试了 len=1000),并且不会损坏内存。我的版本工作的原因可能是
new float*[len]
,您可以将其重写为malloc(len*sizeof(float*))
尝试一下这是否是问题所在。(我手头没有c++编译器,无法自己尝试)
顺便说一句,我发现有趣的是,您在 dct2() 中
new
,但在 doublecpy() 中delete
。反正。这是我改编的代码:
I modified your discrete cosine transform and butterfly code to fit my C compiler (not C++). I inserted dummy-functions and constants for those that i dont have. It works (i tested it up to len=1000) without corrupting memory. The reason my version works may be
new float*[len]
, which you could rewrite tomalloc(len*sizeof(float*))
to try out if that was the problem.(I dont have a c++-compiler handy to try it myself)
By the way, i find it intersting that you
new
in dct2() butdelete
in doublecpy().Anyway. This is my adapted code:
正如我在评论中所说,我看不出有什么问题......
尝试使用 duma 等工具来检查损坏情况。 ..my2c
As said in my comment, I can not see anything wrong ...
Try tools like duma to check for corruption ...
my2c
我开始将所有数组转换为 std::vector ,并在中途运行代码。这并没有解决我的问题,但我找到了错误的原因。就在这个函数中:
错误非常明显,我纠正了它:
虽然这很奇怪。我预计在运行以下行中的错误代码时会收到“数组索引越界”或“分段错误”: mat[i][j] = arr[i][j];
无论如何,感谢大家的帮助。
I started converting all arrays to std::vector and in the midway i ran the code. That didn't solve my problem but i found the cause of the error. it was in this function:
The mistake is very visible, i corrected it:
Although this is very weird. I expected to receive an "array index out of bound" or "segmentation fault" when running the wrong code in the line: mat[i][j] = arr[i][j];
Anyways thank you all for your help.