segmentation fault help needed..please. 
July 2nd, 2009, 07:57 AM
| | Newbie | | Join Date: Jul 2009
Posts: 4
| |
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! - #include <NTL/ZZ.h>
-
#include <NTL/vec_ZZ.h>
-
#include <cstdlib>
-
-
NTL_CLIENT
-
-
#define SIZE_OF_INTEGER 10
-
#define BASE_1 2
-
#define BASE_2 3
-
#define NUM 10
-
-
struct entry_table {
-
ZZ num;
-
long m,n;
-
};
-
-
ZZ bs ( entry_table a[], long beg, long end, ZZ nbr )
-
{
-
long mid;
-
mid = ( beg + end ) / 2;
-
if ( nbr == a[mid].num )
-
{
-
cout << " 2{" << a[mid].m << "}*3{" << a[mid].n <<"} + ";
-
return a[mid].num;
-
}
-
else if ( beg > end )
-
{
-
cout <<" 2{" << a[mid].m << "}*3{" << a[mid].n << "} + ";
-
return a[end].num;
-
}
-
else if ( nbr < a[mid].num )
-
{
-
end = mid - 1;
-
return bs ( a, beg, end, nbr );
-
}
-
else if ( nbr > a[mid].num )
-
{
-
beg = mid + 1;
-
return bs ( a, beg, end, nbr );
-
}
-
}
-
-
ZZ alg ( entry_table a[], long x, ZZ numbr, ZZ count )
-
{
-
ZZ num1;
-
num1 = numbr - bs ( a, 0, x, numbr );
-
if ( num1 == 0 )
-
return count;
-
else
-
return alg ( a, x, num1, ++count );
-
-
}
-
-
entry_table quick ( entry_table a[], long begin, long end )
-
{
-
long num = end - begin + 1;
-
if ( num > 2 )
-
{
-
long left = begin + 1;
-
long right = end;
-
while ( left < right )
-
{
-
while ( left <= end && a[left].num <= a[begin].num )
-
left++;
-
while ( a[right].num > a[begin].num )
-
right--;
-
if ( left < right )
-
swap ( a[left], a[right] );
-
}
-
swap ( a[begin], a[right] );
-
quick ( a, begin, right - 1 );
-
quick ( a, right + 1, end );
-
}
-
else if ( num == 2 )
-
{
-
if ( a[end].num < a[begin].num )
-
swap( a[end], a[begin] );
-
}
-
}
-
-
entry_table quick ( entry_table a[], long x )
-
{
-
quick ( a, 0, x - 1 );
-
}
-
-
main()
-
{
-
cout<<"gdfhc";//this line isnt even printed so thats why i dont know where and why there is a segfault
-
srand( time(NULL) );
-
SetSeed( to_ZZ(rand()) );
-
ZZ d;
-
vec_ZZ b,c;
-
b.SetLength(NUM);
-
c.SetLength(NUM);
-
long size = SIZE_OF_INTEGER * SIZE_OF_INTEGER;
-
struct entry_table a[ size ];
-
ZZ v;
-
v=power_ZZ ( 2,SIZE_OF_INTEGER );
-
long x=0;
-
for ( long i = 0; i <= SIZE_OF_INTEGER; i++ )
-
{
-
for ( long j = 0; ( d = power_ZZ(BASE_1,i)*power_ZZ(BASE_2,j) ) <= v ; j++ )
-
{
-
a[x].num = d;
-
a[x].m = i;
-
a[x].n = j;
-
x++;
-
}
-
}
-
quick ( a, x );
-
ZZ cnt;
-
cnt = 0;
-
for ( long k = 0; k < NUM; k++ )
-
{
-
c[k] = 1;
-
b[k] = RandomBnd(v);
-
cout << endl << b[k] << " : ";
-
c[k] = alg ( a, x, b[k], c[k] );
-
cnt = cnt + c[k];
-
cout << endl;
-
}
-
cout << endl << "total no. of terms: " << cnt << endl;
-
cout << "avg.= " << cnt/NUM << endl;
-
}
-
| 
July 2nd, 2009, 11:26 AM
| | Needs Regular Fix | | Join Date: Jul 2008
Posts: 367
Provided Answers: 2 | | | 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.
| 
July 2nd, 2009, 05:29 PM
| | Newbie | | Join Date: Jul 2009
Posts: 4
| | | re: segmentation fault help needed..please.
i did..see the "cout" right at the start of main()...
it doesnt even print that
| 
July 2nd, 2009, 05:53 PM
|  | Expert | | Join Date: Mar 2007
Posts: 10,634
Provided Answers: 2 | | | re: segmentation fault help needed..please. Quote:
Originally Posted by pooh1119 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
| 
July 2nd, 2009, 06:02 PM
| | Newbie | | Join Date: Jul 2009
Posts: 4
| | | re: segmentation fault help needed..please.
just tried it..still doesnt print..
is there somethin wrong with the sizes of the structure arrays?
| 
July 3rd, 2009, 08:52 AM
| | Needs Regular Fix | | Join Date: Jul 2008
Posts: 367
Provided Answers: 2 | | | re: segmentation fault help needed..please.
Does 'helloworld' program segfault too?
| 
July 3rd, 2009, 10:22 AM
| | Newbie | | Join Date: Jul 2009
Posts: 4
| | | 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: - gdfhc
-
*** glibc detected *** ./p1: free(): invalid pointer: 0x00007fff770db658 ***
-
======= Backtrace: =========
-
/lib/libc.so.6[0x7fae6e43608a]
-
/lib/libc.so.6(cfree+0x8c)[0x7fae6e439c1c]
-
./p1[0x409edb]
-
./p1[0x402965]
-
./p1[0x402bc3]
-
./p1(__gxx_personality_v0+0x457)[0x40168f]
-
./p1[0x401769]
-
./p1[0x40200d]
-
/lib/libc.so.6(__libc_start_main+0xf4)[0x7fae6e3e01c4]
-
./p1(__gxx_personality_v0+0xa1)[0x4012d9]
-
======= Memory map: ========
-
00400000-00418000 r-xp 00000000 08:03 7422089 /home/indu/p1
-
00618000-00619000 rw-p 00018000 08:03 7422089 /home/indu/p1
-
00619000-0063a000 rw-p 00619000 00:00 0 [heap]
-
7fae68000000-7fae68021000 rw-p 7fae68000000 00:00 0
-
7fae68021000-7fae6c000000 ---p 7fae68021000 00:00 0
-
7fae6e3c2000-7fae6e51a000 r-xp 00000000 08:01 32791 /lib/libc-2.7.so
-
7fae6e51a000-7fae6e71a000 ---p 00158000 08:01 32791 /lib/libc-2.7.so
-
7fae6e71a000-7fae6e71d000 r--p 00158000 08:01 32791 /lib/libc-2.7.so
-
7fae6e71d000-7fae6e71f000 rw-p 0015b000 08:01 32791 /lib/libc-2.7.so
-
7fae6e71f000-7fae6e724000 rw-p 7fae6e71f000 00:00 0
-
7fae6e724000-7fae6e731000 r-xp 00000000 08:01 32784 /lib/libgcc_s.so.1
-
7fae6e731000-7fae6e931000 ---p 0000d000 08:01 32784 /lib/libgcc_s.so.1
-
7fae6e931000-7fae6e932000 rw-p 0000d000 08:01 32784 /lib/libgcc_s.so.1
-
7fae6e932000-7fae6e9b2000 r-xp 00000000 08:01 32795 /lib/libm-2.7.so
-
7fae6e9b2000-7fae6ebb1000 ---p 00080000 08:01 32795 /lib/libm-2.7.so
-
7fae6ebb1000-7fae6ebb3000 rw-p 0007f000 08:01 32795 /lib/libm-2.7.so
-
7fae6ebb3000-7fae6eca2000 r-xp 00000000 08:01 1360820 /usr/lib/libstdc++.so.6.0.9
-
7fae6eca2000-7fae6eea2000 ---p 000ef000 08:01 1360820 /usr/lib/libstdc++.so.6.0.9
-
7fae6eea2000-7fae6eea8000 r--p 000ef000 08:01 1360820 /usr/lib/libstdc++.so.6.0.9
-
7fae6eea8000-7fae6eeab000 rw-p 000f5000 08:01 1360820 /usr/lib/libstdc++.so.6.0.9
-
7fae6eeab000-7fae6eebe000 rw-p 7fae6eeab000 00:00 0
-
7fae6eebe000-7fae6eedb000 r-xp 00000000 08:01 32785 /lib/ld-2.7.so
-
7fae6f0c5000-7fae6f0c8000 rw-p 7fae6f0c5000 00:00 0
-
7fae6f0d7000-7fae6f0db000 rw-p 7fae6f0d7000 00:00 0
-
7fae6f0db000-7fae6f0dd000 rw-p 0001d000 08:01 32785 /lib/ld-2.7.so
-
7fff770c8000-7fff770dd000 rw-p 7ffffffea000 00:00 0 [stack]
-
7fff771fe000-7fff77200000 r-xp 7fff771fe000 00:00 0 [vdso]
-
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
-
Aborted
-
| 
July 3rd, 2009, 04:05 PM
| | Expert | | Join Date: Mar 2008 Location: Naperville, Illinois U.S.
Posts: 804
Provided Answers: 3 | | | re: segmentation fault help needed..please. Quote:
Originally Posted by newb16 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 ...
|  | | | | /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 225,662 network members.
|