heap corruption in c++
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.
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.
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 to malloc(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() but delete
in doublecpy().
Anyway. This is my adapted code:
#include <stdio.h>
#include <stdlib.h>
#define len 1000
#define PI 3.14
float mat[len][len];
float a(int u) {
return 1.0;
}
void doublecpy(float** d){
int i;
for(i = 0; i < len; i++){
int j;
for(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(i = 0; i < len; i++)
free(d[i]);
free(d);
}
void dct2(){
float** G = malloc(len*sizeof(float*));//new float* [len];
int u;
for(u = 0; u < len; u++){
G[u] = malloc(len*sizeof(float));
int v;
for(v = 0; v < len; v++){
G[u][v] = 0;
int x;
for(x = 0; x < len; x++){
int y;
for(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);
}
int main()
{
dct2();
getch();
}
As said in my comment, I can not see anything wrong ...
Try tools like duma to check for corruption ...
my2c
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:
void cpy(int** arr, int x, int y){
for(int i = x; i < x + len; i++){
for(int j = y; j < y + len; j++){
mat[i][j] = arr[i][j];
}
}
}
The mistake is very visible, i corrected it:
void cpy(int** arr, int x, int y){
for(int i = 0; i < len; i++){
for(int j = 0; j < len; j++){
mat[i][j] = arr[i + x][j + y];
}
}
}
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.
精彩评论