MHB Solve Segmentation Fault: Destroy Planetary System "Solid

  • Thread starter Thread starter mathmari
  • Start date Start date
  • Tags Tags
    Fault
Click For Summary
The discussion revolves around troubleshooting a segmentation fault in a C function designed to destroy a planetary system and manage asteroids. The key issue arises when accessing the `ffplan` array, where the index `j` may be out of range, leading to the segmentation fault. Participants suggest using assertions to verify that all indices are valid and recommend using a debugger like gdb to trace the program's execution for better insight into the error. The importance of correctly using the `exit` function is emphasized, as failing to include parentheses can cause the program to continue running unexpectedly. Overall, the conversation highlights debugging strategies and the need for careful index management in linked data structures.
  • #31
mathmari said:
Code:
	int ffplanpos=-1;
       
        ...
	

	for(i=0; i<max; i++){
		if(StarS[j].ffplan[i] == INT_MAX){
			ffplanpos=i;
		}
	 }
	
	if(ffplanpos == -1){
		return -1;
	}

Is it better now?? (Wondering)

Yep! (Smile)
 
Technology news on Phys.org
  • #32
I like Serena said:
Yep! (Smile)

When I compile it I get an error at the line [m]if(StarS[j].ffplan == INT_MAX){[/m]

[m]error: invalid operands to binary ==[/m]

What does this mean?? (Wondering)
 
  • #33
mathmari said:
When I compile it I get an error at the line [m]if(StarS[j].ffplan == INT_MAX){[/m]

[m]error: invalid operands to binary ==[/m]

What does this mean?? (Wondering)


It means that we cannot compare StarS[j].ffplan to INT_MAX.
Is StarS[j].ffplan an integer?
Or is it perhaps a structure from which we should select a data member? (Wondering)
 
  • #34
I like Serena said:
It means that we cannot compare StarS[j].ffplan to INT_MAX.
Is StarS[j].ffplan an integer?
Or is it perhaps a structure from which we should select a data member? (Wondering)


It is a structure which has the fields [m]fp[/m] (Identifier of the free-floating planet) and [m]ff[/m] (Pointer to the first node of the list of the free-floating planets).

So do we have to write it as followed??

Code:
int ffplanpos=-1;
       
        ...
	

	for(i=0; i<max; i++){
		if(StarS[j].ffplan[i].fp == INT_MAX){
			ffplanpos=i;
		}
	 }
	
	if(ffplanpos == -1){
		return -1;
	}

        StarS[j].ffplan[ffplanpos].fp=solid;

(Wondering)
 
  • #35
mathmari said:
It is a structure which has the fields [m]fp[/m] (Identifier of the free-floating planet) and [m]ff[/m] (Pointer to the first node of the list of the free-floating planets).

So do we have to write it as followed??

Code:
int ffplanpos=-1;
       
        ...
	

	for(i=0; i<max; i++){
		if(StarS[j].ffplan[i].fp == INT_MAX){
			ffplanpos=i;
		}
	 }
	
	if(ffplanpos == -1){
		return -1;
	}

        StarS[j].ffplan[ffplanpos].fp=solid;

(Wondering)

I do not understand yet what [m]ff[/m] should represent, but yes, your code looks correct now. (Smile)
 
  • #36
I like Serena said:
I do not understand yet what [m]ff[/m] should represent, but yes, your code looks correct now. (Smile)

[m]ff[/m] is a pointer of type asteroid_t to the first element of a doubly linked list. This list is the list of the free-floating planets of the collection of the free-foating planets with identifier fp. Each element of the list of the free-floating planets of a collection of free-floating planets is a struct of type asteroid_t.
 
  • #37
mathmari said:
[m]ff[/m] is a pointer of type asteroid_t to the first element of a doubly linked list. This list is the list of the free-floating planets of the collection of the free-foating planets with identifier fp. Each element of the list of the free-floating planets of a collection of free-floating planets is a struct of type asteroid_t.

Ah okay. That explains it. (Smile)
 
  • #38
When I compile the program I get a segmentation fault...

Code:
int destruction(int solid, int gap){
	int ffplanpos=-1;
	int i, j;
	int sum=0;
	asteroid_t *planf = calloc(1, sizeof(asteroid_t));
	asteroid_t *K=NULL;
	asteroid_t *f=NULL;
	plansys_t *p=StarS[0].plasy;
	for(i=0; i<Sfreep; i++){
		p=StarS[i].plasy;
		while (p != NULL && p->solid != solid){
			p=p->next;
			j=i;
		}
	}
	if(p == NULL){
		printf("The planetary system with identifier %d couldn't be found\n", solid);
		return -1;
	}
	f=p->asteroids;
	while(sum<gap){
		sum=sum+f->gap;
		f=f->next;
		DELETE(f->prev, f->prev->as);
	}
	

	for(i=0; i<max; i++){
		if(StarS[j].ffplan[i].fp == INT_MAX){
			ffplanpos=i;
		}
	}
	
	if(ffplanpos == -1){
		return -1;
	}
	
	StarS[j].ffplan[ffplanpos].fp=solid;
	
	
	planf->as=f->as;
	planf->gap=0;
	planf->next=NULL;
	planf->prev=NULL;	f=f->next;
	while(f != NULL){
		if (StarS[j].ffplan[ffplanpos].ff == NULL) {
        	       StarS[j].ffplan[ffplanpos].ff = planf;
                } else {
        	       asteroid_t *last = StarS[j].ffplan[ffplanpos].ff;
        	       while (last->next != NULL) {
        		       last = last->next;
        	       }
        	       last->next = planf;
       	        }     
		f=f->next;
	}     
	
	
	printf("\n\nPlanet Systems = ");
	
	while(StarS[j].plasy != NULL){
		printf(" %d ", StarS[j].plasy->solid);
		StarS[j].plasy=StarS[j].plasy->next;
	}
	
	printf("\n\nFree-floatingM = ");
	
	for(i=0; i<ffplanpos; i++){
		printf(" %d ", StarS[j].ffplan[i].fp);
	}
	
	printf("\n\nFree-floating planets = ");

	
	printf("\n\n %d ", StarS[j].ffplan[0].ff->as);
	for(i=0; i<=ffplanpos; i++){
		while(StarS[j].ffplan[i].ff != NULL){
			printf(" %d ", StarS[j].ffplan[i].ff->as);
			StarS[j].ffplan[i].ff=StarS[j].ffplan[i].ff->next;
		}
	}

	return 0;
	
}

What could I have done wrong?? (Wondering)
 
  • #39
mathmari said:
When I compile the program I get a segmentation fault...

What could I have done wrong?? (Wondering)

Where do you get the segmentation fault? (Wondering)

One of the ways to find out, is to run:
[m]\$ gdb stars
(gdb) r
<crash>
(gdb) bt[/m]
This will show where the program was, and where it was coming from when the segmentation fault occurred. (Wasntme)
 
  • #40
I like Serena said:
Where do you get the segmentation fault? (Wondering)

One of the ways to find out, is to run:
[m]\$ gdb stars
(gdb) r
<crash>
(gdb) bt[/m]
This will show where the program was, and where it was coming from when the segmentation fault occurred. (Wasntme)

When I write [m]r[/m] I get :

[m] Starting program:
No executable specified, use 'target exec'.[/m]What does this mean?? (Wondering)

Also what am I supposed to write at the point [m]<crash>[/m] ?? (Wondering)
 
  • #41
mathmari said:
When I write [m]r[/m] I get :

[m] Starting program:
No executable specified, use 'target exec'.[/m]What does this mean?? (Wondering)

Also what am I supposed to write at the point [m]<crash>[/m] ?? (Wondering)

What is the name of your program? (Wondering)
That's what you should type after gdb in the first line.

<crash> means that you should wait for the crash to happen. (Wasntme)
 
  • #42
I like Serena said:
What is the name of your program? (Wondering)
That's what you should type after gdb in the first line.

Should I write [m](gdb) main[/m] or [m](gdb) stars[/m] or something else that contains both of them?? (Wondering)
 
  • #43
mathmari said:
Should I write [m](gdb) main[/m] or [m](gdb) stars[/m] or something else that contains both of them?? (Wondering)

Which command do you use to build your program? (Wondering)

Let's assume it is [m]gcc stars.c[/m].
This generates an executable named [m]a.out[/m].

Then you should use:
[m]gdb a.out[/m]
This will start gdb with your executable, and a [m](gdb)[/m] prompt will appear. (Thinking)

Enter [m]r[/m], and your program starts running until it presumably crashes with a segmentation fault.

At that time gdb will show where it crashed.
And if you enter [m]bt[/m], it will give more detailed information. (Mmm)
 
  • #44
I like Serena said:
Which command do you use to build your program? (Wondering)

Let's assume it is [m]gcc stars.c[/m].

I compile the program using the following:

[m]gcc main.c stars.c[/m]

(Wondering)
 
  • #45
mathmari said:
I compile the program using the following:

[m]gcc main.c stars.c[/m]

(Wondering)

Then you will also get [m]a.out[/m] as resulting executable.
So the way to use gdb is the same. (Mmm)
 
  • #46
I like Serena said:
Then you will also get [m]a.out[/m] as resulting executable.
So the way to use gdb is the same. (Mmm)

So, should I write the following commands??

[m]gcc main.c stars.c
gdb a.out
(gdb) r[/m]

(Wondering)
 
  • #47
mathmari said:
So, should I write the following commands??

[m]gcc main.c stars.c
gdb a.out
(gdb) r[/m]

(Wondering)

Hold on.
You need to compile with [m]gcc -g main.c stars.c[/m]

What do you get when you do? (Wondering)
 
  • #48
I like Serena said:
Hold on.
You need to compile with [m]gcc -g main.c stars.c[/m]

What do you get when you do? (Wondering)

Do you mean that after [m]gcc -g main.c stars.c[/m] I should write
[m]gdb a.out
(gdb) r[/m] ?? (Wondering)
 
  • #49
mathmari said:
Do you mean that after [m]gcc -g main.c stars.c[/m] I should write
[m]gdb a.out
(gdb) r[/m] ?? (Wondering)

Yes...
 
  • #50
I like Serena said:
Yes...

When I write the command [m]gdb a.out[/m] I get:

[m]a.out: No such file or directory.[/m]

(Wondering)
 
  • #51
mathmari said:
When I write the command [m]gdb a.out[/m] I get:

[m]a.out: No such file or directory.[/m]

(Wondering)

How do you normally run your executable? (Wondering)
 
  • #52
Try "gdb ./a.out". Note the period before "/".
 
  • #53
I wrote [m]gdb a.exe[/m].

Then I wrote the command [m](gdb) r[/m] and I get the following:

[m]Starting program: /cygdrive/c/cygwin/a.exe
[New thread 3764.0x50c]
Error: dll starting at 0x76c60000 not found.
Error: dll starting at 0x75c30000 not found.
Error: dll starting at 0x76c60000 not found.
Error: dll starting at 0x76b60000 not found.
[New thread 3764.0x4c4]
Usage: /a <input_file>

Program exited with code 01.[/m]

What does this mean?? (Wondering)
 
  • #54
mathmari said:
I wrote [m]gdb a.exe[/m].

Then I wrote the command [m](gdb) r[/m] and I get the following:

[m]Starting program: /cygdrive/c/cygwin/a.exe
[New thread 3764.0x50c]
Error: dll starting at 0x76c60000 not found.
Error: dll starting at 0x75c30000 not found.
Error: dll starting at 0x76c60000 not found.
Error: dll starting at 0x76b60000 not found.
[New thread 3764.0x4c4]
Usage: /a <input_file>

Program exited with code 01.[/m]

What does this mean?? (Wondering)

Did you compile with [m]-g[/m]? (Wondering)
 
  • #55
I like Serena said:
Did you compile with [m]-g[/m]? (Wondering)

Yes, I compiled it as followed:

[m]gcc -g main.c stars.c[/m]
 
  • #56
mathmari said:
Yes, I compiled it as followed:

[m]gcc -g main.c stars.c[/m]

What do you get if you type [m]gcc -v[/m]? (Wondering)
And what for [m] gdb -v[/m]?
 
  • #57
I like Serena said:
What do you get if you type [m]gcc -v[/m]? (Wondering)

Do you mean that I have to type [m]gcc -v[/m] without [m]main.c stars.c[/m] ?? (Wondering)
 
  • #58
mathmari said:
Do you mean that I have to type [m]gcc -v[/m] without [m]main.c stars.c[/m] ?? (Wondering)

Yes.

It will give the version and configuration of the gcc respectively the gdb version you are using.
It appears they are somehow not compatible. :confused:

Btw, didn't you have gdb running properly before? (Wondering)
 
  • #59
When I type [m]gcc -v[/m] I get the following:

[m]Reading specs from /usr/lib/gcc/i686-pc-cygwin/3.4.4/specs
Configured with: /managed/gcc-build/final-v3-bootstrap/gcc-3.4.4-999/configure --verbose --program-suffix=-3 --prefix=/usr --exec-prefix=/usr --sysconfdir=/etc --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --enable-languages=c,ada,c++,d,f77,pascal,java,objc --enable-nls --without-included-gettext --enable-version-specific-runtime-libs --without-x --enable-libgcj --disable-java-awt --with-system-zlib --enable-interpreter --disable-libgcj-debug --enable-threads=posix --enable-java-gc=boehm --disable-win32-registry --enable-sjlj-exceptions --enable-hash-synchronization --enable-libstdcxx-debug
Thread model: posix
gcc version 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)[/m]
When I type [m]gdb -v[/m] I get the following:

[m]GNU gdb 6.8.0.20080328-cvs <cygwin-special>
Copyright <C> 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <301 Moved Permanently>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details.
This GDB was configured as "i686-pc-cygwin".[/m]
 
  • #60
When I check the same thing, I get:
[m]\$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/lto-wrapper.exe
...
gcc version 4.8.3 (GCC)[/m]

[m]\$ gdb -v
GNU gdb (GDB) 7.8
...
This GDB was configured as "x86_64-pc-cygwin".
[/m]

What is drawing my attention is that you appear to have a "cygming special".
Do you perhaps have the so called MinGW version of the gcc compiler? (Wondering)
If so, perhaps that may be incompatible with the version of gdb that you have.
(Not to mention that your version is apparently pretty old.)

Ideally, you'd use the setup of Cygwin to install both gcc and gdb.
If you do it like that, both should work in conjunction with each other. (Thinking)

Otherwise, we'll have to use a more primitive way to figure out where your segmentation fault is coming from. (Sweating)
 

Similar threads

Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 20 ·
Replies
20
Views
5K
  • · Replies 5 ·
Replies
5
Views
4K