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:
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.
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:
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.
That should have worked, but next time just use memcpy():
1 2 3 4 5 6 7 8 9 10 11
float *work_data1 = newfloat[lpfC * lpfR];
float *work_data2 = newfloat[hpfC * hpfR];
//Initialize work_data1 and 2 here
....
//Now allocate and copy (although I don't see the need):
retval->lpf = newfloat[lpfC * lpfR];
retval->hpf = newfloat[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.