Hi, I wrote a program to do some computation of complicated polynomials. But there are some problems. I use gdb to debug the program. When the input are small, there is no problem.

When the input are large or many factors have the same subscript (for example, input 5 for m, then input 7 8 9 6 8 9 5 8 9 4 7 8 9 3 4 5, then 5 for n, then 4 5 6 7 8 9 6 7 8 5 6 7 4 8 9, then first part of the program can work, m=Y^{9}_{3,4}Y^{4}_{7,8}Y^{5}_{8,9}Y^{6}_{8,9}Y^{7}_{8,9}

simplify(m)=Y^{9}_{3,4}Y^{4}_{7,8}Y^{11}_{8,9}

n=Y^{4}_{5,6}Y^{5}_{6,7}Y^{6}_{7,8}Y^{4}_{8,9}Y^{7}_{8,9}

FM=Y^{9}_{3,4}Y^{4}_{7,8}Y^{11}_{8,9}

), the following segmentation fault occurs. I do not know how to fix it. Thank you very much.

Program received signal SIGSEGV, Segmentation fault.

0x0019d98a in ?? () from /lib/libc.so.6

(gdb) backtrace

#0 0x0019d98a in ?? () from /lib/libc.so.6

#1 0x0019ff33 in malloc () from /lib/libc.so.6

#2 0x08048a89 in initial_polynomial () at test18.c:38

#3 0x080497ba in add_polynomials (a=0x804d148, b=0x804d128) at test18.c:708

#4 0x08049b01 in main (argc=1, argv=0xbffff3f4) at test18.c:823

(gdb) frame 3

#3 0x080497ba in add_polynomials (a=0x804d148, b=0x804d128) at test18.c:708

708 polynomial head=initial_polynomial();

(gdb) l

703

704

705

706 polynomial add_polynomials(polynomial a, polynomial b)

707 {

708 polynomial head=initial_polynomial();

709 polynomial_node *c=NULL, *p=NULL, *s=NULL;

710

711 p=head;

712

(gdb) print head

$1 = (polynomial) 0x804d148

(gdb) print head->next->m

$2 = (monomial) 0x804d008

(gdb) print head->next->m->y

$3 = {l = 0, i = 0, k = 0}

(gdb) print s->m->next->y

Cannot access memory at address 0xfbad2a94

(gdb) print s->m->next

Cannot access memory at address 0xfbad2a94

(gdb) print s->m

$4 = (monomial) 0xfbad2a84

When the input are large or many factors have the same subscript (for example, input 5 for m, then input 7 8 9 6 8 9 5 8 9 4 7 8 9 3 4 5, then 5 for n, then 4 5 6 7 8 9 6 7 8 5 6 7 4 8 9, then first part of the program can work, m=Y^{9}_{3,4}Y^{4}_{7,8}Y^{5}_{8,9}Y^{6}_{8,9}Y^{7}_{8,9}

simplify(m)=Y^{9}_{3,4}Y^{4}_{7,8}Y^{11}_{8,9}

n=Y^{4}_{5,6}Y^{5}_{6,7}Y^{6}_{7,8}Y^{4}_{8,9}Y^{7}_{8,9}

FM=Y^{9}_{3,4}Y^{4}_{7,8}Y^{11}_{8,9}

), the following segmentation fault occurs. I do not know how to fix it. Thank you very much.

Program received signal SIGSEGV, Segmentation fault.

0x0019d98a in ?? () from /lib/libc.so.6

(gdb) backtrace

#0 0x0019d98a in ?? () from /lib/libc.so.6

#1 0x0019ff33 in malloc () from /lib/libc.so.6

#2 0x08048a89 in initial_polynomial () at test18.c:38

#3 0x080497ba in add_polynomials (a=0x804d148, b=0x804d128) at test18.c:708

#4 0x08049b01 in main (argc=1, argv=0xbffff3f4) at test18.c:823

(gdb) frame 3

#3 0x080497ba in add_polynomials (a=0x804d148, b=0x804d128) at test18.c:708

708 polynomial head=initial_polynomial();

(gdb) l

703

704

705

706 polynomial add_polynomials(polynomial a, polynomial b)

707 {

708 polynomial head=initial_polynomial();

709 polynomial_node *c=NULL, *p=NULL, *s=NULL;

710

711 p=head;

712

(gdb) print head

$1 = (polynomial) 0x804d148

(gdb) print head->next->m

$2 = (monomial) 0x804d008

(gdb) print head->next->m->y

$3 = {l = 0, i = 0, k = 0}

(gdb) print s->m->next->y

Cannot access memory at address 0xfbad2a94

(gdb) print s->m->next

Cannot access memory at address 0xfbad2a94

(gdb) print s->m

$4 = (monomial) 0xfbad2a84

Last edited: