Connecting Tech Pros Worldwide Forums | Help | Site Map

SIGSEGV on delete[] problem, recursive function calls

Newbie
 
Join Date: Sep 2009
Location: Berlin, Germany
Posts: 2
#1: Sep 27 '09
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.

Expand|Select|Wrap|Line Numbers
  1. int coeff2data_remaining (double * coeffs, int number_c, int next_c, compled * data, int m, int i, compled * primitive_root, int next_pp){
  2.     //handle case of small number_c, no recursive call
  3.     cout << "coeff2data_remaining (" <<coeffs<<","<<number_c<<", "<<next_c<<", "<<data<<", "<<m<<", "<<i<<", "<<primitive_root<<", "<<next_pp<<")"<<endl;
  4.     if (number_c == 1){
  5.         int index_data=0;
  6.         for (int index_point = 1; index_point < m; ++index_point)
  7.             if (index_point%i)
  8.                 data[index_data++]=*coeffs;
  9.         return 0;
  10.     }
  11.     //group the coefficients in evens and odds, call this function recursively
  12.     int half = m>>1;
  13.     int number_evens = number_c>>1;
  14.     int number_odds = (number_evens<<1 == number_c)? number_evens : number_evens+1;
  15.     compled * data_evens = new compled [half-half/i];
  16.     cout << "allocated data_evens: " <<data_evens<<", size: "<<(half-half/i)<<endl;
  17.     compled * data_odds = new compled [half-half/i];
  18.     cout << "allocated data_odds: " <<data_odds<<", size: "<<(half-half/i)<<endl;
  19.     cout << "calling coeff2data_remaining twice ..."<<endl;
  20.     coeff2data_remaining (coeffs, number_evens, next_c<<1, data_evens, m>>1, i, primitive_root, next_pp<<1);
  21.     coeff2data_remaining (coeffs+next_c, number_odds, next_c<<1, data_odds, m>>1, i, primitive_root, next_pp<<1);
  22.  
  23.     //use each received data twice to calculate the desired data
  24.     int index_data=0;
  25.     int index_pp=next_pp;
  26.     compled diff;
  27.     for (int index_point = 1; index_point<half; ++index_point, index_pp+=next_pp)
  28.         if (index_point%i){
  29.             diff = primitive_root[index_pp]*data_odds[index_data];
  30.             data[index_data]=data_evens[index_data]+diff;
  31.             data[half+index_data]=data_evens[index_data]-diff;
  32.             ++index_data;
  33.         }
  34.  
  35.     //free allocated memory
  36.     cout << "deleting data_evens: " <<data_evens<<endl;
  37.     delete [] data_evens;
  38.     cout << "deleting data_odds: " <<data_odds<<endl;
  39.     delete [] data_odds;
  40.     cout << "successful end of coeff2data_remaining (" <<coeffs<<","<<number_c<<", "<<next_c<<", "<<data<<", "<<m<<", "<<i<<", "<<primitive_root<<", "<<next_pp<<")"<<endl;
  41.     return 0;
  42. }
Running my program produces the following output. (The function is called twice from the outside before crashing.)

Expand|Select|Wrap|Line Numbers
  1. coeff2data_remaining (0x9ddc228,2, 1, 0x9ddc2f0, 4, 2, 0x9ddc008, 2)
  2. allocated data_evens: 0x9ddc340, size: 1
  3. allocated data_odds: 0x9ddc358, size: 1
  4. calling coeff2data_remaining twice ...
  5. coeff2data_remaining (0x9ddc228,1, 2, 0x9ddc340, 2, 2, 0x9ddc008, 4)
  6. coeff2data_remaining (0x9ddc230,1, 2, 0x9ddc358, 2, 2, 0x9ddc008, 4)
  7. deleting data_evens: 0x9ddc340
  8. deleting data_odds: 0x9ddc358
  9. successful end of coeff2data_remaining (0x9ddc228,2, 1, 0x9ddc2f0, 4, 2, 0x9ddc008, 2)
  10. coeff2data_remaining (0x9ddc240,2, 1, 0x9ddc318, 4, 2, 0x9ddc008, 2)
  11. allocated data_evens: 0x9ddc358, size: 1
  12. allocated data_odds: 0x9ddc340, size: 1
  13. calling coeff2data_remaining twice ...
  14. coeff2data_remaining (0x9ddc240,1, 2, 0x9ddc358, 2, 2, 0x9ddc008, 4)
  15. coeff2data_remaining (0x9ddc248,1, 2, 0x9ddc340, 2, 2, 0x9ddc008, 4)
  16. deleting data_evens: 0x9ddc358
  17. 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
#2: Sep 28 '09

re: SIGSEGV on delete[] problem, recursive function calls


Hi everyone,
I believe to have found the error.
In line 31 it wrote
Expand|Select|Wrap|Line Numbers
  1. 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
Expand|Select|Wrap|Line Numbers
  1. 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
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,208
#3: Sep 28 '09

re: SIGSEGV on delete[] problem, recursive function calls


Quote:

Originally Posted by presencia View Post

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.
Reply

Tags
delete, recursive, sigsegv