Connecting Tech Pros Worldwide Forums | Help | Site Map

segmentation fault help needed..please.

Newbie
 
Join Date: Jul 2009
Posts: 4
#1: Jul 2 '09
hi.
my code below makes use of NTL(a library for doin number theory available at www.shoup.net); im trying to print the double base number representation of any large number(32 bits and/or more) but my program gives me a segmentation fault. i was unable to find out where though. could some please tell me where the fault is and how to correct it?
Thanks!

Expand|Select|Wrap|Line Numbers
  1. #include <NTL/ZZ.h>
  2. #include <NTL/vec_ZZ.h>
  3. #include <cstdlib>
  4.  
  5. NTL_CLIENT
  6.  
  7. #define SIZE_OF_INTEGER 10
  8. #define BASE_1 2
  9. #define BASE_2 3
  10. #define NUM 10
  11.  
  12. struct entry_table {
  13.         ZZ num;
  14.         long m,n;
  15. };
  16.  
  17. ZZ bs ( entry_table a[], long beg, long end, ZZ nbr )
  18. {
  19.         long mid;
  20.         mid = ( beg + end ) / 2;
  21.         if ( nbr == a[mid].num )
  22.         {
  23.                 cout << " 2{" << a[mid].m << "}*3{" << a[mid].n <<"} + ";
  24.                 return a[mid].num;
  25.         }
  26.         else if ( beg > end )
  27.         {
  28.                 cout <<" 2{" << a[mid].m << "}*3{" << a[mid].n << "} + ";
  29.                 return a[end].num;
  30.         }
  31.         else if ( nbr < a[mid].num )
  32.         {
  33.                 end = mid - 1;
  34.                 return bs ( a, beg, end, nbr );
  35.         }
  36.         else if ( nbr > a[mid].num )
  37.         {
  38.                 beg = mid + 1;
  39.                 return bs ( a, beg, end, nbr );
  40.         }
  41. }
  42.  
  43. ZZ alg ( entry_table a[], long x, ZZ numbr, ZZ count )
  44. {
  45.         ZZ num1;
  46.         num1 = numbr - bs ( a, 0, x, numbr );
  47.         if ( num1 == 0 )
  48.                 return count;
  49.         else
  50.                 return alg ( a, x, num1, ++count );
  51.  
  52. }
  53.  
  54. entry_table quick ( entry_table a[], long begin, long end  )
  55. {
  56.         long num = end - begin + 1;
  57.         if ( num > 2 )
  58.         {
  59.                 long left = begin + 1;
  60.                 long right = end;
  61.                 while ( left < right )
  62.                 {
  63.                         while ( left <= end && a[left].num <= a[begin].num )
  64.                                 left++;
  65.                         while ( a[right].num > a[begin].num )
  66.                                 right--;
  67.                         if ( left < right )
  68.                                 swap ( a[left], a[right] );
  69.                 }
  70.                 swap ( a[begin], a[right] );
  71.                 quick ( a, begin, right - 1 );
  72.                 quick ( a, right + 1, end );
  73.     }
  74.     else if ( num == 2 )
  75.     {
  76.                 if ( a[end].num < a[begin].num )
  77.                         swap( a[end], a[begin] );
  78.     }
  79. }
  80.  
  81. entry_table quick ( entry_table a[], long x )
  82. {
  83.         quick ( a, 0, x - 1 );
  84. }
  85.  
  86. main()
  87. {
  88.         cout<<"gdfhc";//this line isnt even printed so thats why i dont know where and why there is a segfault
  89.         srand( time(NULL) );
  90.         SetSeed( to_ZZ(rand()) );
  91.         ZZ d;
  92.         vec_ZZ b,c;
  93.         b.SetLength(NUM);
  94.         c.SetLength(NUM);
  95.         long size = SIZE_OF_INTEGER * SIZE_OF_INTEGER;
  96.         struct entry_table a[ size ];
  97.         ZZ v;
  98.         v=power_ZZ ( 2,SIZE_OF_INTEGER );
  99.         long x=0;
  100.         for ( long i = 0; i <= SIZE_OF_INTEGER; i++ )
  101.         {
  102.                 for ( long j = 0; ( d = power_ZZ(BASE_1,i)*power_ZZ(BASE_2,j) ) <= v ; j++ )
  103.                 {
  104.                                 a[x].num = d;
  105.                                 a[x].m = i;
  106.                                 a[x].n = j;
  107.                                 x++;
  108.                 }
  109.         }
  110.         quick ( a, x );
  111.         ZZ cnt;
  112.         cnt = 0;
  113.         for ( long k = 0; k < NUM; k++ )
  114.         {
  115.                 c[k] = 1;
  116.                 b[k] = RandomBnd(v);
  117.                 cout << endl << b[k] << " : ";
  118.                 c[k] = alg ( a, x, b[k], c[k] );
  119.                 cnt = cnt + c[k];
  120.                 cout << endl;
  121.         }
  122.         cout << endl << "total no. of terms: " << cnt << endl;
  123.         cout << "avg.= " << cnt/NUM << endl;
  124. }
  125.  

Needs Regular Fix
 
Join Date: Jul 2008
Posts: 383
#2: Jul 2 '09

re: segmentation fault help needed..please.


If you have no debugger, try to stuff your code with debug prints. The last one before crash may hint you about crash location.
Newbie
 
Join Date: Jul 2009
Posts: 4
#3: Jul 2 '09

re: segmentation fault help needed..please.


i did..see the "cout" right at the start of main()...
it doesnt even print that
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#4: Jul 2 '09

re: segmentation fault help needed..please.


Quote:

Originally Posted by pooh1119 View Post

i did..see the "cout" right at the start of main()...
it doesnt even print that

The standard output stream is probably line buffered; print out "text\n" instead and see if it is printed.

kind regards,

Jos
Newbie
 
Join Date: Jul 2009
Posts: 4
#5: Jul 2 '09

re: segmentation fault help needed..please.


just tried it..still doesnt print..
is there somethin wrong with the sizes of the structure arrays?
Needs Regular Fix
 
Join Date: Jul 2008
Posts: 383
#6: Jul 3 '09

re: segmentation fault help needed..please.


Does 'helloworld' program segfault too?
Newbie
 
Join Date: Jul 2009
Posts: 4
#7: Jul 3 '09

re: segmentation fault help needed..please.


no..helloworld works just fine
n now actually the first "cout" statement of "main()" appears n then i get a list of different memory adresses withe "aborted" in the end
like this:
Expand|Select|Wrap|Line Numbers
  1. gdfhc
  2. *** glibc detected *** ./p1: free(): invalid pointer: 0x00007fff770db658 ***
  3. ======= Backtrace: =========
  4. /lib/libc.so.6[0x7fae6e43608a]
  5. /lib/libc.so.6(cfree+0x8c)[0x7fae6e439c1c]
  6. ./p1[0x409edb]
  7. ./p1[0x402965]
  8. ./p1[0x402bc3]
  9. ./p1(__gxx_personality_v0+0x457)[0x40168f]
  10. ./p1[0x401769]
  11. ./p1[0x40200d]
  12. /lib/libc.so.6(__libc_start_main+0xf4)[0x7fae6e3e01c4]
  13. ./p1(__gxx_personality_v0+0xa1)[0x4012d9]
  14. ======= Memory map: ========
  15. 00400000-00418000 r-xp 00000000 08:03 7422089                            /home/indu/p1
  16. 00618000-00619000 rw-p 00018000 08:03 7422089                            /home/indu/p1
  17. 00619000-0063a000 rw-p 00619000 00:00 0                                  [heap]
  18. 7fae68000000-7fae68021000 rw-p 7fae68000000 00:00 0 
  19. 7fae68021000-7fae6c000000 ---p 7fae68021000 00:00 0 
  20. 7fae6e3c2000-7fae6e51a000 r-xp 00000000 08:01 32791                      /lib/libc-2.7.so
  21. 7fae6e51a000-7fae6e71a000 ---p 00158000 08:01 32791                      /lib/libc-2.7.so
  22. 7fae6e71a000-7fae6e71d000 r--p 00158000 08:01 32791                      /lib/libc-2.7.so
  23. 7fae6e71d000-7fae6e71f000 rw-p 0015b000 08:01 32791                      /lib/libc-2.7.so
  24. 7fae6e71f000-7fae6e724000 rw-p 7fae6e71f000 00:00 0 
  25. 7fae6e724000-7fae6e731000 r-xp 00000000 08:01 32784                      /lib/libgcc_s.so.1
  26. 7fae6e731000-7fae6e931000 ---p 0000d000 08:01 32784                      /lib/libgcc_s.so.1
  27. 7fae6e931000-7fae6e932000 rw-p 0000d000 08:01 32784                      /lib/libgcc_s.so.1
  28. 7fae6e932000-7fae6e9b2000 r-xp 00000000 08:01 32795                      /lib/libm-2.7.so
  29. 7fae6e9b2000-7fae6ebb1000 ---p 00080000 08:01 32795                      /lib/libm-2.7.so
  30. 7fae6ebb1000-7fae6ebb3000 rw-p 0007f000 08:01 32795                      /lib/libm-2.7.so
  31. 7fae6ebb3000-7fae6eca2000 r-xp 00000000 08:01 1360820                    /usr/lib/libstdc++.so.6.0.9
  32. 7fae6eca2000-7fae6eea2000 ---p 000ef000 08:01 1360820                    /usr/lib/libstdc++.so.6.0.9
  33. 7fae6eea2000-7fae6eea8000 r--p 000ef000 08:01 1360820                    /usr/lib/libstdc++.so.6.0.9
  34. 7fae6eea8000-7fae6eeab000 rw-p 000f5000 08:01 1360820                    /usr/lib/libstdc++.so.6.0.9
  35. 7fae6eeab000-7fae6eebe000 rw-p 7fae6eeab000 00:00 0 
  36. 7fae6eebe000-7fae6eedb000 r-xp 00000000 08:01 32785                      /lib/ld-2.7.so
  37. 7fae6f0c5000-7fae6f0c8000 rw-p 7fae6f0c5000 00:00 0 
  38. 7fae6f0d7000-7fae6f0db000 rw-p 7fae6f0d7000 00:00 0 
  39. 7fae6f0db000-7fae6f0dd000 rw-p 0001d000 08:01 32785                      /lib/ld-2.7.so
  40. 7fff770c8000-7fff770dd000 rw-p 7ffffffea000 00:00 0                      [stack]
  41. 7fff771fe000-7fff77200000 r-xp 7fff771fe000 00:00 0                      [vdso]
  42. ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
  43. Aborted
  44.  
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 831
#8: Jul 3 '09

re: segmentation fault help needed..please.


Quote:

Originally Posted by newb16 View Post

If you have no debugger, try to stuff your code with debug prints. The last one before crash may hint you about crash location.

Suggest you follow Newb16's advice and sprinkle debug prints throughout main to locate the culprit. Then you can instrument that function; and so on ...
Reply