SIGSEGV on delete[] problem, recursive function calls | Newbie | | Join Date: Sep 2009 Location: Berlin, Germany
Posts: 2
| |
Hi everyone,
I have written a small program running fine until a Segmentation fault. Using a debugger I have localized the function the fault happens in. The Signal appears during a call to delete[] in the recursive function I have posted below. I have done some C coding before but I am fairly new to C++. I would really appreciate any help. I can't figure out where the problem is.
The function the SIGSEGV appears in is the following. It receives various pointers to arrays, compled is defined by "typedef complex<double> compled", the numbers m and i are powers of 2 and m/2 > i. I have inserted some output commands to track the problem. - int coeff2data_remaining (double * coeffs, int number_c, int next_c, compled * data, int m, int i, compled * primitive_root, int next_pp){
-
//handle case of small number_c, no recursive call
-
cout << "coeff2data_remaining (" <<coeffs<<","<<number_c<<", "<<next_c<<", "<<data<<", "<<m<<", "<<i<<", "<<primitive_root<<", "<<next_pp<<")"<<endl;
-
if (number_c == 1){
-
int index_data=0;
-
for (int index_point = 1; index_point < m; ++index_point)
-
if (index_point%i)
-
data[index_data++]=*coeffs;
-
return 0;
-
}
-
//group the coefficients in evens and odds, call this function recursively
-
int half = m>>1;
-
int number_evens = number_c>>1;
-
int number_odds = (number_evens<<1 == number_c)? number_evens : number_evens+1;
-
compled * data_evens = new compled [half-half/i];
-
cout << "allocated data_evens: " <<data_evens<<", size: "<<(half-half/i)<<endl;
-
compled * data_odds = new compled [half-half/i];
-
cout << "allocated data_odds: " <<data_odds<<", size: "<<(half-half/i)<<endl;
-
cout << "calling coeff2data_remaining twice ..."<<endl;
-
coeff2data_remaining (coeffs, number_evens, next_c<<1, data_evens, m>>1, i, primitive_root, next_pp<<1);
-
coeff2data_remaining (coeffs+next_c, number_odds, next_c<<1, data_odds, m>>1, i, primitive_root, next_pp<<1);
-
-
//use each received data twice to calculate the desired data
-
int index_data=0;
-
int index_pp=next_pp;
-
compled diff;
-
for (int index_point = 1; index_point<half; ++index_point, index_pp+=next_pp)
-
if (index_point%i){
-
diff = primitive_root[index_pp]*data_odds[index_data];
-
data[index_data]=data_evens[index_data]+diff;
-
data[half+index_data]=data_evens[index_data]-diff;
-
++index_data;
-
}
-
-
//free allocated memory
-
cout << "deleting data_evens: " <<data_evens<<endl;
-
delete [] data_evens;
-
cout << "deleting data_odds: " <<data_odds<<endl;
-
delete [] data_odds;
-
cout << "successful end of coeff2data_remaining (" <<coeffs<<","<<number_c<<", "<<next_c<<", "<<data<<", "<<m<<", "<<i<<", "<<primitive_root<<", "<<next_pp<<")"<<endl;
-
return 0;
-
}
Running my program produces the following output. (The function is called twice from the outside before crashing.) - coeff2data_remaining (0x9ddc228,2, 1, 0x9ddc2f0, 4, 2, 0x9ddc008, 2)
-
allocated data_evens: 0x9ddc340, size: 1
-
allocated data_odds: 0x9ddc358, size: 1
-
calling coeff2data_remaining twice ...
-
coeff2data_remaining (0x9ddc228,1, 2, 0x9ddc340, 2, 2, 0x9ddc008, 4)
-
coeff2data_remaining (0x9ddc230,1, 2, 0x9ddc358, 2, 2, 0x9ddc008, 4)
-
deleting data_evens: 0x9ddc340
-
deleting data_odds: 0x9ddc358
-
successful end of coeff2data_remaining (0x9ddc228,2, 1, 0x9ddc2f0, 4, 2, 0x9ddc008, 2)
-
coeff2data_remaining (0x9ddc240,2, 1, 0x9ddc318, 4, 2, 0x9ddc008, 2)
-
allocated data_evens: 0x9ddc358, size: 1
-
allocated data_odds: 0x9ddc340, size: 1
-
calling coeff2data_remaining twice ...
-
coeff2data_remaining (0x9ddc240,1, 2, 0x9ddc358, 2, 2, 0x9ddc008, 4)
-
coeff2data_remaining (0x9ddc248,1, 2, 0x9ddc340, 2, 2, 0x9ddc008, 4)
-
deleting data_evens: 0x9ddc358
-
deleting data_odds: 0x9ddc340
As I said, any helpful comment is greatly appreciated. And I will be happy to privide any further information if needed.
~<><~~~~~ presencia
| | Newbie | | Join Date: Sep 2009 Location: Berlin, Germany
Posts: 2
| | | re: SIGSEGV on delete[] problem, recursive function calls
Hi everyone,
I believe to have found the error.
In line 31 it wrote - data[half+index_data]=data_evens[index_data]-diff;
which was wrong, because I was writing "to the array" outside of its allocated bound. I probably overwrote some information needed for the delete of an array, therefore the program crashed processing the delete.
The line should have been - data[half+index_data-half/i]=data_evens[index_data]-diff;
since before the call the data array had been allocated to have length (half*2)-(half*2)/i in line 15 or 17, respectively. Since the index_data will go to half-half/i - 1, everything should work out fine now.
Maybe I shoud have started all this using vectors with the at() function instead of raw arrays. I was just so used to arrays and didn't see much of a point there.
Thanks to everyone who has looked at this thread trying to help.
This thread can be closed.
~<><~~~~~ presencia
|  | AdministratorVoR | | Join Date: Feb 2006 Location: South West UK
Posts: 6,208
| | | re: SIGSEGV on delete[] problem, recursive function calls Quote:
Originally Posted by presencia Maybe I shoud have started all this using vectors with the at() function instead of raw arrays. I was just so used to arrays and didn't see much of a point there. If you are using C++ then you should always use vectors instead of raw arrays. The overhead of using a vector is negligible, there can be a little overhead in creating the vector but a lot of that can be mitigated by using the vector correctly.
Hanging on to using arrays instead of vectors is indicative of someone semi-stuck in the C world but using C++, I know it took me ages to make the switch from defaulting to arrays to defaulting to vectors.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,561 network members.
|