Solve Segmentation Fault: Destroy Planetary System "Solid

  • MHB
  • Thread starter mathmari
  • Start date
  • Tags
    Fault
In summary: Wondering)In summary, the conversation is about writing a function for the destruction of a planetary system and troubleshooting a segmentation fault in the code. The expert suggests using assert commands before the problematic code and adding fflush(NULL) after printf to check for any issues. The use of a debugger like gdb is also suggested to further analyze and debug the code.
  • #106
I like Serena said:
Then that list is empty for some reason. (Thinking)

But if this list were also [m]NULL[/m], wouldn't we have to get the following:
[m]The planetary system doesn't contain any asteroids[/m] ?? (Wondering)
Code:
if(p->asteroids == NULL){
		printf("The planetary system doesn't contain any asteroids\n");
		return -1;
	}
 
Technology news on Phys.org
  • #107
mathmari said:
But if this list were also [m]NULL[/m], wouldn't we have to get the following:
[m]The planetary system doesn't contain any asteroids[/m] ?? (Wondering)
Code:
if(p->asteroids == NULL){
		printf("The planetary system doesn't contain any asteroids\n");
		return -1;
	}

Then the value of [m]p->asteroids[/m] must have changed after that if-statement. (Sweating)
What happened to it? (Wondering)
 
  • #108
I like Serena said:
Then the value of [m]p->asteroids[/m] must have changed after that if-statement. (Sweating)
What happened to it? (Wondering)

But direct after the if-statement we set it to [m]f[/m].

Code:
if(p->asteroids == NULL){
		printf("The planetary system doesn't contain any asteroids\n");
		return -1;
	}
	
	
	f=p->asteroids;

(Wondering)
 
  • #109
mathmari said:
But direct after the if-statement we set it to [m]f[/m].

Code:
if(p->asteroids == NULL){
		printf("The planetary system doesn't contain any asteroids\n");
		return -1;
	}
	
	
	f=p->asteroids;

(Wondering)

And what happens afterwards with [m]f[/m]? (Thinking)
 
  • #110
I like Serena said:
And what happens afterwards with [m]f[/m]? (Thinking)

After that we delete elements from the list as long as the sum of gaps is less than the given gap.

Code:
while(sum<gap && f != NULL){
    	sum=sum+f->gap;
    	next=f->next;
    	free(f);    
    	f = next;
}
p->asteroids = f;
if (f != NULL) {
        f->prev=NULL;
}
 
  • #111
mathmari said:
After that we delete elements from the list as long as the sum of gaps is less than the given gap.

Code:
while(sum<gap && f != NULL){
    	sum=sum+f->gap;
    	next=f->next;
    	free(f);    
    	f = next;
}
p->asteroids = f;
if (f != NULL) {
        f->prev=NULL;
}

I see... (Thinking)

So afterwards f points to the remainder of the list... which might be empty! :eek:
 
  • #112
I like Serena said:
I see... (Thinking)

So afterwards f points to the remainder of the list... which might be empty! :eek:

So, can we do it as followed??

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

	i=0;
	while(i<max && StarS[j].ffplan[i].fp != INT_MAX){
		i=i+1;
	}

	ffplanpos=i;
	

	if(ffplanpos == -1){
		return -1;
	}
	
	StarS[j].ffplan[ffplanpos].fp=solid;
	
	[COLOR="#FF0000"]if(f != NULL){[/COLOR]
	       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;
	      }     
	
      [COLOR="#FF0000"]}[/COLOR]	

        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 = ");

	
	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;
	
}

(Wondering)
 
  • #113
mathmari said:
So, can we do it as followed?? (Wondering)

I think that would take care of the segmentation fault yes. (Smile)

How about the program logic?
Does it still do what it is supposed to do? (Wondering)
 
  • #114
I like Serena said:
I think that would take care of the segmentation fault yes. (Smile)

(Smile)
I like Serena said:
How about the program logic?
Does it still do what it is supposed to do? (Wondering)

I don't know because it prints the following:

[m]Planet Systems = 7620213 7620203
Free-floatingM = 7620203
Free-floating planets =[/m]

It doesn't print anything at Free-floating planets, because [m]f[/m] is [m]NULL[/m], right?? (Wondering)

But having the following in the file.txt

[m]I
C 762
B 7620213 762
B 7620203 762
A 987 4232 7620203
A 985 1232 7620203
A 986 6344 7620203
D 7620203 5000[/m]

we have created three asteroids with gaps 4232, 1232, 6344.

So, should the last asteroid get a free-floating planet, since with [m]D 7620203 5000[/m] we want to convert these asteroids into free-floating planets that have gap greater than 5000, or not?? (Wondering)
 
  • #115
mathmari said:
So, should the last asteroid get a free-floating planet, since with [m]D 7620203 5000[/m] we want to convert these asteroids into free-floating planets that have gap greater than 5000, or not?? (Wondering)

Or have I understood it wrong?? (Wondering)
 
  • #116
mathmari said:
we have created three asteroids with gaps 4232, 1232, 6344.

So, should the last asteroid get a free-floating planet, since with [m]D 7620203 5000[/m] we want to convert these asteroids into free-floating planets that have gap greater than 5000, or not?? (Wondering)

I think you are right.
So it seems something is still going wrong with the destruction of the asteriods - it appears one too many is destroyed. (Sweating)
 
  • #117
I like Serena said:
So it seems something is still going wrong with the destruction of the asteriods - it appears one too many is destroyed. (Sweating)

But why does this occur?? (Wondering)
 
  • #118
mathmari said:
But why does this occur?? (Wondering)

We should zoom in on the code that decides which ones to destroy! (Nod)
What is the code? (Wondering)
And what should it do? (Wondering)
 
  • #119
I like Serena said:
We should zoom in on the code that decides which ones to destroy! (Nod)
What is the code? (Wondering)
And what should it do? (Wondering)

Code:
	f=p->asteroids;
	
	while(sum<gap && f != NULL){
    	sum=sum+f->gap;
    	next=f->next;
    	free(f);    
    	f = next;
	}
	p->asteroids = f;
	if (f != NULL) {
    	f->prev=NULL;
	}

	i=0;
	while(i<max && StarS[j].ffplan[i].fp != INT_MAX){
		i=i+1;
	}

	ffplanpos=i;
	

	if(ffplanpos == -1){
		return -1;
	}
	
	StarS[j].ffplan[ffplanpos].fp=solid;
	
	if(f != NULL){
	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;
	}     
	
}

I made some changes:

Code:
	f=p->asteroids;
	sum=f->gap;
	while(sum<gap && f != NULL){
    	       next=f->next;
    	       free(f);    
    	       f = next;
               sum=sum+f->gap;
	}
	p->asteroids = f;
	if (f != NULL) {
    	       f->prev=NULL;
	}

	i=0;
	while(i<max && StarS[j].ffplan[i].fp != INT_MAX){
		i=i+1;
	}

	ffplanpos=i;
	

	if(ffplanpos == -1){
		return -1;
	}
	
	StarS[j].ffplan[ffplanpos].fp=solid;
	
	if(f != NULL){
	        planf->as=f->as;
	        planf->gap=0;
	        planf->next=NULL;
	        planf->prev=NULL;

	        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;
	}     
	
}
Now it prints the following:
[m]Planet Systems = 7620203 7620213
Free-floatingM = 7620203
Free-floating planets = 986[/m]

So, is it correct now?? (Wondering)
 
  • #120
Or is still something wrong?? (Wondering)
 

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
20
Views
4K
  • Sci-Fi Writing and World Building
Replies
11
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
3K
Replies
1
Views
975
  • Astronomy and Astrophysics
Replies
2
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
2K
Back
Top