Structure Data Corruption

Jul 20, 2011 at 3:57pm
Hi there,
I seem to be having data corruption within a structure. In a function in which I manipulate data I have tested the contents of the function and they seem to be accurate, however after the data passes back to the higher level function the first 8 elements get corrupted somehow. I have tried to pass the structure by reference as well as try to return the structure. Both have the same issue.
The structure is defined as follows:
1
2
3
4
5
struct afb2d_Adata{
int hpfR,hpfC,lpfR,lpfC;
float *lpf;
float *hpf;
}


I have my central function that does the following:
1
2
3
4
5
void rootfunc(){
afb2d_Adata LH;
func1(&LH);
for (int i=0;i<LH.lpfC*LH.lpfR;i++) std::cout<<LH.lpf[i]<<" "; //THIS SHOWS CORRUPTION
}


And func1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
func1(afb2d_Adata *retval)
{
float *work_data1=new float[lpfC*lpfR];
float *work_data2=new float[hpfC*hpfR];
//do processes on work_data1&2
//set retval->lpfC;
//set retval->lpfR;
//set retval->hpfR;
//set retval->hpfC;
retval->lpf=new float[lpfC*lpfR];
retval->hpf=new float[hpfC*hpfR];
retval->lpf=work_data1;
retval->hpf=work_data2;
for(int i=0;i<lpfC*lpfR;i++) std::cout<<retval->lpf[i]<<" "; //THIS WORKS ACCURATELY
}


Any words of wisdom? Your help is GREATLY appreciated!
Thanks,
eth0.
Jul 20, 2011 at 4:14pm
Lines 10 and 11 for func1() are memory leaks: You allocate arrays of floats, but then you discard the pointers without deleting the arrays, which you are not using in the first place.

But that doesn't affect the result of the for loop in the end of the function. As it is in your code, func1() doesn't show any data initialization, meaning you are outputting garbage values because the allocated arrays are never initialized.

Even after this, I don't see how the code you show can corrupt the arrays.
Jul 20, 2011 at 4:26pm
Hi webJose,
Thanks for the quick response. The data in work_data1 & work_data2 are set in the commented section. Also I omitted a few pieces of code to simplify.
I do have the following in func1:
1
2
3
4
5
retval->lpf=work_data1;
retval->hpf=work_data1;
delete []work_data1;
delete []work_data2;
for(int i=0;i<lpfC*lpfR;i++) std::cout<<retval->lpf[i]<<" "; //THIS WORKS ACCURATELY 


Also, if it helps I notice that it is ALWAYS the first 8 elements that seem to get corrupted. While I should see data like:
299.03 299.04 299.006 299.004 299.03 299.04 299.006 299.004  ...........

(which is what outputs at the end of func1)
Instead I see the following from rootfun():
1.53741e-38 0 -877602 4.5793e-41 0 0 0 0.............

Jul 20, 2011 at 4:30pm
Deleting work_data1 and work_data2 is effectively deleting retval->lpf and retval->hpf. I think you need to review the concept of pointers and dynamic memory because you are destroying what you compute. Why does it work inside the function? I am guessing by mere luck.
Jul 20, 2011 at 4:37pm
Instead of doing:
1
2
retval->lpf=work_data1;
retval->hpf=work_data1;


I had initially done the following, but it segfaulted without much description:
1
2
for(int i=0;i<lpfC*lpfR;i++) retval->lpf[i]=work_data1[i];
for(int j=0;j<hpfC*hpfR;j++) retval->hpf[j]=work_data2[j];


I tried gdb, but since the app runs 2 threads it just gives me the function in error(which I already know)
Last edited on Jul 20, 2011 at 4:40pm
Jul 20, 2011 at 4:48pm
That should have worked, but next time just use memcpy():

1
2
3
4
5
6
7
8
9
10
11
float *work_data1 = new float[lpfC * lpfR];
float *work_data2 = new float[hpfC * hpfR];
//Initialize work_data1 and 2 here
....
//Now allocate and copy (although I don't see the need):
retval->lpf = new float[lpfC * lpfR];
retval->hpf = new float[hpfC * hpfR];
memcpy(retval->lpf, work_data1, lpfC * lpfR * sizeof(float));
memcpy(retval->hpf, work_data2, hpfC * hpfR * sizeof(float));
delete[] work_data1;
delete[] work_data2;


That would be the way of doing it, but it doesn' make much sense: Instead of duplicating work_data1 and work_data2, you should just hand over the pointers (my means of assignment) and don't delete[] them.
Jul 20, 2011 at 4:52pm
Awesome, that fixed it. Thanks for the help and the tip!
Topic archived. No new replies allowed.