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.
  • #71
mathmari said:
[m]The planetary system doesn't contain any asteroids
D 7620203 5000 failed
[Thread 5184.0x798 exited with code 0]
[Inferior 1 (process 5184) exited normally][/m]

But why doesn't it contain any asteroids?? (Wondering)

Didn't we add the asteroids with the following function??

Which information do you have in your [m]file.txt[/m] regarding solid=7620203 and gap=5000? (Wondering)

Suppose you add:
Code:
	for(i=0; i<Sfreep; i++){
		p=StarS[i].plasy;
		while (p != NULL && p->solid != solid){
			p=p->next;
		}
	}
        [color=red]printf("asteroid_constitution(as=%d, gap=%d, solid=%d) p=%p\n",
                as, gap, solid, p);[/color]
	if (p->asteroids==NULL){

What is the output that you get regarding solid=7620203? (Wondering)
 
Technology news on Phys.org
  • #72
I like Serena said:
Which information do you have in your [m]file.txt[/m] regarding solid=7620203 and gap=5000? (Wondering)

The file.txt is the following:

[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]

When I write [m](gdb) r[/m] I get the following:

[m]Starting program: /cygdrive/c/cygwin/a.exe file.txt
[New Thread 6136.0xcc0]
[New Thread 6136.0x1408]

I OK
C 762
StarSystems = 762
OK
B 7620213 762
PlanetarySystems = 7620213
OK
B 7620203 762
PlanetarySystems = 7620213 7620203
OK
A 987 4232 7620203
987 : 4232 : 0 : 0

OK
A 985 1232 7620203
985 : 1232 : 0 : 0

OK
A 986 6344 7620203
986 : 6344 : 0 : 0

OK
The planetary system doesn't contain any asteroids
D 7620203 5000 failed
[Thread 6136.0x1408 exited with code 0]
[Inferior 1 (process 6136) exited normally][/m]
I like Serena said:
Suppose you add:
Code:
	for(i=0; i<Sfreep; i++){
		p=StarS[i].plasy;
		while (p != NULL && p->solid != solid){
			p=p->next;
		}
	}
        [color=red]printf("asteroid_constitution(as=%d, gap=%d, solid=%d) p=%p\n",
                as, gap, solid, p);[/color]
	if (p->asteroids==NULL){

What is the output that you get regarding solid=7620203? (Wondering)

When I add the command [m]printf("asteroid_constitution(as=%d, gap=%d, solid=%d) p=%p\n",
as, gap, solid, p);[/m]
I get the following:

[m]Starting program: /cygdrive/c/cygwin/a.exe file.txt
[New Thread 6052.0x14f4]
[New Thread 6052.0xe80]

I OK
C 762
StarSystems = 762
OK
B 7620213 762
PlanetarySystems = 7620213
OK
B 7620203 762
PlanetarySystems = 7620213 7620203
OK
asteroid_constitution(as=987, gap=4232, solid=7620203) p=0x80059918
A 987 4232 7620203
987 : 4232 : 0 : 0

OK
asteroid_constitution(as=985, gap=1232, solid=7620203) p=0x80059918
A 985 1232 7620203
985 : 1232 : 0 : 0

OK
asteroid_constitution(as=986, gap=6344, solid=7620203) p=0x80059918
A 986 6344 7620203
986 : 6344 : 0 : 0

OK
The planetary system doesn't contain any asteroids
D 7620203 5000 failed
[Thread 6052.0xe80 exited with code 0]
[Inferior 1 (process 6052) exited normally][/m]

(Wondering)
 
Last edited by a moderator:
  • #73
mathmari said:
When I add the command [m]printf("asteroid_constitution(as=%d, gap=%d, solid=%d) p=%p\n",
as, gap, solid, p);[/m]
I get the following:
(Wondering)

I believe this is the location where your problem is.
The code to find the right [m]p[/m] is wrong.
It will always choose the last star system, instead of the one with the matching [m]as[/m]. (Worried)
 
  • #74
I like Serena said:
I believe this is the location where your problem is.
The code to find the right [m]p[/m] is wrong.
It will always choose the last star system, instead of the one with the matching [m]as[/m]. (Worried)

I changed something...

Code:
for(i=0; i<Sfreep; i++){
		p=StarS[i].plasy;
		while ([COLOR="#FF0000"]p->next[/COLOR] != NULL && p->solid != solid){
			p=p->next;
			[COLOR="#FF0000"]k=i;[/COLOR]
		}
	}
	
[COLOR="#FF0000"]p=StarS[k].plasy;[/COLOR]
	
printf("asteroid_constitution(as=%d, gap=%d, solid=%d) p=%p\n",
                as, gap, solid, p);

Now I get the following:

[m]Starting program: /cygdrive/c/cygwin/a.exe file.txt
[New Thread 4860.0x1668]
[New Thread 4860.0x13d0]

I OK
C 762
StarSystems = 762
OK
B 7620213 762
PlanetarySystems = 7620213
OK
B 7620203 762
PlanetarySystems = 7620213 7620203
OK
asteroid_constitution(as=987, gap=4232, solid=7620203) p=0x80059908
A 987 4232 7620203
987 : 4232 : 0 : 0

OK
asteroid_constitution(as=985, gap=1232, solid=7620203) p=0x80059908
A 985 1232 7620203
985 : 1232 : 0 : 0

OK
asteroid_constitution(as=986, gap=6344, solid=7620203) p=0x80059908
A 986 6344 7620203
986 : 6344 : 0 : 0

OK
The planetary system doesn't contain any asteroids
D 7620203 5000 failed
[Thread 4860.0x13d0 exited with code 0]
[Inferior 1 (process 4860) exited normally][/m]
 
  • #75
mathmari said:
I changed something...

Code:
for(i=0; i<Sfreep; i++){
		p=StarS[i].plasy;
		while ([COLOR="#FF0000"]p->next[/COLOR] != NULL && p->solid != solid){
			p=p->next;
			[COLOR="#FF0000"]k=i;[/COLOR]
		}
	}
	
[COLOR="#FF0000"]p=StarS[k].plasy;[/COLOR]
	
printf("asteroid_constitution(as=%d, gap=%d, solid=%d) p=%p\n",
                as, gap, solid, p);

This will get you the last star system with at least 1 planetary system.
It will not get you the one with the matching [m]solid[/m] identification. (Doh)
 
  • #76
I like Serena said:
This will get you the last star system with at least 1 planetary system.
It will not get you the one with the matching [m]solid[/m] identification. (Doh)

I added a check...

Code:
        int k=-1;
        ...
	for(i=0; i<Sfreep; i++){
		p=StarS[i].plasy;
		while (p->next != NULL && p->solid != solid){
			p=p->next;
			k=i;
		}
		if(p->solid == solid){
			k=i;
		}
	}
	if(k == -1){
		return -1;
	}
	
	p=StarS[k].plasy;
	
        printf("asteroid_constitution(as=%d, gap=%d, solid=%d) p=%p\n",
                as, gap, solid, p);

Do do we get now the right one?? (Wondering)
 
  • #77
mathmari said:
I added a check...
Do do we get now the right one?? (Wondering)

Let's see... (Thinking)

Code:
		p=StarS[i].plasy;
		while (p->next != NULL && p->solid != solid){

What if [m]StarS.plasy[/m] is NULL?
Then we can't use [m]p->next[/m]. (Worried)

Code:
			p=p->next;
			k=i;

Aren't you always setting [m]k[/m] here? Even if we did not find [m]solid[/m]? (Wondering)
 
  • #78
I like Serena said:
What if [m]StarS.plasy[/m] is NULL?
Then we can't use [m]p->next[/m]. (Worried)

Code:
			p=p->next;
			k=i;

Aren't you always setting [m]k[/m] here? Even if we did not find [m]solid[/m]? (Wondering)


Code:
     int k=-1;
        ...
	for(i=0; i<Sfreep; i++){
		p=StarS[i].plasy;
		while (p != NULL && p->next != NULL && p->solid != solid){
			p=p->next;
		}
		if(p->solid == solid){
			k=i;
		}
	}
	if(k == -1){
		return -1;
	}
	
	p=StarS[k].plasy;
	
        printf("asteroid_constitution(as=%d, gap=%d, solid=%d) p=%p\n",
                as, gap, solid, p);

Is it better now?? (Wondering)
 
  • #79
mathmari said:
Code:
     int k=-1;
        ...
	for(i=0; i<Sfreep; i++){
		p=StarS[i].plasy;
		while (p != NULL && p->next != NULL && p->solid != solid){
			p=p->next;
		}
		if(p->solid == solid){
			k=i;
		}
	}
	if(k == -1){
		return -1;
	}
	
	p=StarS[k].plasy;
	
        printf("asteroid_constitution(as=%d, gap=%d, solid=%d) p=%p\n",
                as, gap, solid, p);

Is it better now?? (Wondering)

Better yes... but what will happen if some [m]StarS.plasy[/m] is NULL?
I think you'll still have a segmentation fault. (Worried)

Btw, the check [m]p->next != NULL[/m] is redundant, although it does make the logic more complicated. (Nerd)
 
  • #80
I like Serena said:
Better yes... but what will happen if some [m]StarS.plasy[/m] is NULL?
I think you'll still have a segmentation fault. (Worried)


If we add [m]p != NULL[/m] in the if statement, would still get a segmentation fault if some [m]StarS.plasy[/m] is NULL?? (Wondering)

Code:
int k=-1;
        ...
	for(i=0; i<Sfreep; i++){
		p=StarS[i].plasy;
		[COLOR="#FF0000"]while (p != NULL && p->solid != solid)[/COLOR]{
			p=p->next;
		}
		[COLOR="#FF0000"]if(p != NULL && p->solid == solid)[/COLOR]{
			k=i;
		}
	}
	if(k == -1){
		return -1;
	}
	
	p=StarS[k].plasy;
	
        printf("asteroid_constitution(as=%d, gap=%d, solid=%d) p=%p\n",
                as, gap, solid, p);
I like Serena said:
Btw, the check [m]p->next != NULL[/m] is redundant, although it does make the logic more complicated. (Nerd)

But when we leave the while loop don't we have set a [m]p[/m] [m]p->next[/m] which might be [m]NULL[/m] ?? (Wondering)
 
  • #81
mathmari said:
If we add [m]p != NULL[/m] in the if statement, would still get a segmentation fault if some [m]StarS.plasy[/m] is NULL?? (Wondering)


No, because C short circuits logical expressions.
If [m]p == NULL[/m] the if-condition is considered false without ever evaluating [m]p->solid == solid[/m]. (Nerd)


Code:
int k=-1;
        ...
	for(i=0; i<Sfreep; i++){
		p=StarS[i].plasy;
		[COLOR="#FF0000"]while (p != NULL && p->solid != solid)[/COLOR]{
			p=p->next;
		}
		[COLOR="#FF0000"]if(p != NULL && p->solid == solid)[/COLOR]{
			k=i;
		}
	}
	if(k == -1){
		return -1;
	}
	
	p=StarS[k].plasy;
	
        printf("asteroid_constitution(as=%d, gap=%d, solid=%d) p=%p\n",
                as, gap, solid, p);

Much better! (Smile)
But when we leave the while loop don't we have set a [m]p[/m] [m]p->next[/m] which might be [m]NULL[/m] ?? (Wondering)

How so? (Wondering)

Within the while-loop, it is always safe to execute [m]p=p->next[/m], and after the while-loop [m]p[/m] points to the planetary system we're interested in.
What else would we want to do with [m]p->next[/m]? (Thinking)
 
  • #82
I like Serena said:
No, because C short circuits logical expressions.
If [m]p == NULL[/m] the if-condition is considered false without ever evaluating [m]p->solid == solid[/m]. (Nerd)

Ahaa... Ok! (Malthe)
I like Serena said:
Much better! (Smile)

(Smile)
I like Serena said:
How so? (Wondering)

Within the while-loop, it is always safe to execute [m]p=p->next[/m], and after the while-loop [m]p[/m] points to the planetary system we're interested in.

I see... (Nerd)

When I add the following at the function

Code:
printf("solid=%p      p->solid==%p \n", solid, p->solid);
	printf("asteroid_constitution(as=%d, gap=%d, solid=%d) p=%p\n",
                as, gap, solid, p);
I get the following: [m]Starting program: /cygdrive/c/cygwin/a.exe file.txt
[New Thread 5100.0x1178]
[New Thread 5100.0x95c]

I OK
C 762
StarSystems = 762
OK
B 7620213 762
PlanetarySystems = 7620213
OK
B 7620203 762
PlanetarySystems = 7620213 7620203
OK
solid=0x74466b p->olsid==0x744675
asteroid_constitution(as=987, gap=4232, solid=7620203) p=0x80059908
A 987 4232 7620203
987 : 4232 : 0 : 0

OK
solid=0x74466b p->solid==0x744675
asteroid_constitution(as=985, gap=1232, solid=7620203) p=0x80059908
A 985 1232 7620203
985 : 1232 : 0 : 0

OK
solid=0x74466b p->solid==0x744675
asteroid_constitution(as=986, gap=6344, solid=7620203) p=0x80059908
A 986 6344 7620203
986 : 6344 : 0 : 0

OK
The planetary system doesn't contain any asteroids
D 7620203 5000 failed
[Thread 5100.0x95c exited with code 0]
[Inferior 1 (process 5100) exited normally][/m]

Does this mean that [m]p[/m] still doesn't point to the correct planetary system?? (Wondering)
 
  • #83
mathmari said:
Does this mean that [m]p[/m] still doesn't point to the correct planetary system?? (Wondering)

What do you get if you use:
Code:
int k=-1;
        ...
	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){
			k=i;
                        [color=red]printf("Found in StarS[%d] solid=%d p->solid==%d StarS[k].plasy=%p\n", 
                               k, solid, p->solid, StarS[k].plasy);[/color]
		}
	}
	if(k == -1){
		return -1;
	}
	
	p=StarS[k].plasy;
	
        [color=red]printf("asteroid_constitution(as=%d, gap=%d, solid=%d) k=%d p=%p\n",
               as, gap, solid, p);[/color]
(Wondering)
 
  • #84
I like Serena said:
What do you get if you use:
Code:
int k=-1;
        ...
	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){
			k=i;
                        [color=red]printf("Found in StarS[%d] solid=%d p->solid==%d StarS[k].plasy=%p\n", 
                               k, solid, p->solid, StarS[k].plasy);[/color]
		}
	}
	if(k == -1){
		return -1;
	}
	
	p=StarS[k].plasy;
	
        [color=red]printf("asteroid_constitution(as=%d, gap=%d, solid=%d) k=%d p=%p\n",
               as, gap, solid, p);[/color]
(Wondering)

I get the following:

[m]Starting program: /cygdrive/c/cygwin/a.exe file.txt
[New Thread 5248.0x1484]
[New Thread 5248.0x14a8]

I OK
C 762
StarSystems = 762
OK
B 7620213 762
PlanetarySystems = 7620213
OK
B 7620203 762
PlanetarySystems = 7620213 7620203
OK
Found in StarS[0] solid=7620203 p->solid==7620203 StarS[k].plasy=0x80059908
asteroid_constitution(as=987, gap=4232, solid=7620203) p=0x80059908
A 987 4232 7620203
987 : 4232 : 0 : 0

OK
Found in StarS[0] solid=7620203 p->solid==7620203 StarS[k].plasy=0x80059908
asteroid_constitution(as=985, gap=1232, solid=7620203) p=0x80059908
A 985 1232 7620203
985 : 1232 : 0 : 0

OK
Found in StarS[0] solid=7620203 p->solid==7620203 StarS[k].plasy=0x80059908
asteroid_constitution(as=986, gap=6344, solid=7620203) p=0x80059908
A 986 6344 7620203
986 : 6344 : 0 : 0

OK
The planetary system doesn't contain any asteroids
D 7620203 5000 failed
[Thread 5248.0x14a8 exited with code 0]
[Inferior 1 (process 5248) exited normally][/m]
 
  • #85
Good! (Happy)

I believe we have confirmed that we're looking at the right star system in the code snippet you provided. (Nod)
That is, the one that matches [m]as[/m], and that contains a planetary system with identification [m]solid[/m].

What's next? (Wondering)
 
  • #86
I like Serena said:
Good! (Happy)

I believe we have confirmed that we're looking at the right star system in the code snippet you provided. (Nod)
That is, the one that matches [m]as[/m], and that contains a planetary system with identification [m]solid[/m].

Great! (Mmm)
I like Serena said:
What's next? (Wondering)

So, now the function asteroid_constitution() is correct and we have to find the error at the function destruction(), right?? (Wondering)
 
  • #87
mathmari said:
So, now the function asteroid_constitution() is correct and we have to find the error at the function destruction(), right?? (Wondering)

It appears that way yes. (Thinking)
 
  • #88
I like Serena said:
It appears that way yes. (Thinking)

I found out that at the function asteroid_constitution() [m]p->asteroids[/m] is [m]NULL[/m].

I wrote the following, at the end of the function:

Code:
        printf("p->asteroids= %p  \n ", p->asteroids);
        printf("A %d %d %d\n", as, gap, solid);
	while(p->planets != NULL){
			 ...
			  p->asteroids=p->asteroids->next;
	 }
	 printf("p->asteroids= %p  \n ", p->asteroids);

I get the following:

[m]Starting program: /cygdrive/c/cygwin/a.exe file.txt
[New Thread 5260.0x1450]
[New Thread 5260.0x1540]

I OK
C 762
StarSystems = 762
OK
B 7620213 762
PlanetarySystems = 7620213
OK
B 7620203 762
PlanetarySystems = 7620213 7620203
OK
p->asteroids= 0x80059928
A 987 4232 7620203
987 : 4232 : 0 : 0
p->asteroids= 0x0

OK
p->asteroids= 0x80059940
A 985 1232 7620203
985 : 1232 : 0 : 0
p->asteroids= 0x0

OK
p->asteroids= 0x80059958
A 986 6344 7620203
986 : 6344 : 0 : 0
p->asteroids= 0x0

OK
The planetary system doesn't contain any asteroids
D 7620203 5000 failed
[Thread 5260.0x1540 exited with code 0]
[Inferior 1 (process 5260) exited normally][/m]

That means that before the while-loop [m]p->asteroids[/m] is not [m]NULL[/m], but after the while-loop it is [m]NULL[/m]. Why does this occur?? (Wondering)
 
  • #89
mathmari said:
I found out that at the function asteroid_constitution() [m]p->asteroids[/m] is [m]NULL[/m].

I wrote the following, at the end of the function:

Code:
        printf("p->asteroids= %p  \n ", p->asteroids);
        printf("A %d %d %d\n", as, gap, solid);
	while(p->planets != NULL){
			 ...
			  p->asteroids=p->asteroids->next;
	 }
	 printf("p->asteroids= %p  \n ", p->asteroids);

Aha! You found something! (Happy)

That means that before the while-loop [m]p->asteroids[/m] is not [m]NULL[/m], but after the while-loop it is [m]NULL[/m]. Why does this occur?? (Wondering)

Because your changing p->asteroids with [m]p->asteroids=p->asteroids->next;[/m].

I guess you wanted to iterate over the linked list, but you're using the head of the list to do so. Afterwards, that head is not valid any more. Effectively, the linked list is now empty. (Worried)
 
  • #90
I like Serena said:
Aha! You found something! (Happy)

(Malthe)
I like Serena said:
Because your changing p->asteroids with [m]p->asteroids=p->asteroids->next;[/m].

I guess you wanted to iterate over the linked list, but you're using the head of the list to do so. Afterwards, that head is not valid any more. Effectively, the linked list is now empty. (Worried)

Yes, I want to iterate over the linked list and print some information.
How can I do it then, without using the head?? (Wondering)
 
  • #91
mathmari said:
(Malthe)

Yes, I want to iterate over the linked list and print some information.
How can I do it then, without using the head?? (Wondering)

Copy the [m]asteroids[/m] data member to a local pointer and use that to iterate. (Mmm)

Also, I suggest to use recognizable names for pointers to important data structures.
It's better to use a name like [m]p[/m] only for iterating over a collection. (Nerd)

So I suggest something like:
Code:
plansys_t *planetarySystem = StarS[k].plasy;
...
asteroid_t *q = planetarySystem ->asteroids;
while (q != NULL) {
    ...
    q = q->next;
}
(Wasntme)
 
  • #92
I like Serena said:
Copy the [m]asteroids[/m] data member to a local pointer and use that to iterate. (Mmm)

Also, I suggest to use recognizable names for pointers to important data structures.
It's better to use a name like [m]p[/m] only for iterating over a collection. (Nerd)

So I suggest something like:
Code:
plansys_t *planetarySystem = StarS[k].plasy;
...
asteroid_t *q = planetarySystem ->asteroids;
while (q != NULL) {
    ...
    q = q->next;
}
(Wasntme)

Ok! Now it isn't [m]NULL[/m] after the while-loop!

Now I get the following:

[m]...
Program received signal SIGSEGV, Segmentation fault.
0x00402593 in destruction (solid=7620203, gap=5000) at stars.c:342
342 DELETE(f->prev, f->prev->as);[/m]

Why do we get a segmentation fault at this point??

Code:
while(sum<gap){
		sum=sum+f->gap;
		f=f->next;
		DELETE(f->prev, f->prev->as);
	}

(Wondering)

The DELETE() function is the following:

Code:
void DELETE(asteroid_t *pointer, int x){
	while(pointer != NULL && pointer->as != x) {
		pointer=pointer->next;
        }
	if(pointer == NULL) {
		printf("Element %d is not present in the list\n", x);
		return;
        } 
	if(pointer->next != NULL){
		pointer->next->prev=pointer->prev;
	}
	if(pointer->prev != NULL){
		pointer->prev->next = pointer->next;
	}
        free(pointer);
}
 
  • #93
mathmari said:
Ok! Now it isn't [m]NULL[/m] after the while-loop!

Good! (Emo)

Why do we get a segmentation fault at this point??

Code:
1. while(sum<gap){
2. 		sum=sum+f->gap;
3. 		f=f->next;
4. 		DELETE(f->prev, f->prev->as);
5. }

(Wondering)

You'll get a segmentation fault if you try to dereference a pointer, when it does not point to a valid location. Typically because it is NULL. (Nerd)

So... how do you know in line 2 and 3 that [m]f[/m] is not a NULL pointer? (Wondering)

And how do you know in line 4, after [m]f=f->next[/m], that [m]f[/m] is not a NULL pointer? (Wondering)
 
  • #94
I like Serena said:
You'll get a segmentation fault if you try to dereference a pointer, when it does not point to a valid location. Typically because it is NULL. (Nerd)

So... how do you know in line 2 and 3 that [m]f[/m] is not a NULL pointer? (Wondering)

And how do you know in line 4, after [m]f=f->next[/m], that [m]f[/m] is not a NULL pointer? (Wondering)

Can we write it as followed??

Code:
while(sum<gap && f->next != NULL){
		sum=sum+f->gap;
		f=f->next;
		DELETE(f->prev, f->prev->as);
}
if(f != NULL){
		DELETE(f, f->as);
}

(Wondering)
 
  • #95
mathmari said:
Can we write it as followed??

Code:
while(sum<gap && f->next != NULL) {
		sum=sum+f->gap;
		f=f->next;
		DELETE(f->prev, f->prev->as);
}
if(f != NULL){
		DELETE(f, f->as);
}

(Wondering)

I consider this suspect code, since we're deleting elements from a list while iterating over it. Moreover the deletion function will iterate on its own. (Thinking)
Furthermore, the special cases where we're deleting the first respectively last element of the list does not appear to be properly accounted for. That appears to be the reason for a segmentation violation. (Worried)

Do you want to delete elements from the list as long as the sum of gaps is less than the given gap? And do you want to start from the beginning of the list? (Wondering)

If so, then I suggest something like:
Code:
sum = 0;
f = head;
while(sum<gap && f != NULL){
    sum=sum+f->gap;
    next=f->next;
    free(f);    
    f = next;
}
head = f;
if (f != NULL) {
    f->prev = NULL;
}
(Wasntme)
 
  • #96
I like Serena said:
Do you want to delete elements from the list as long as the sum of gaps is less than the given gap? And do you want to start from the beginning of the list? (Wondering)

Yes... (Nod)

I like Serena said:
If so, then I suggest something like:
Code:
sum = 0;
f = head;
while(sum<gap && f != NULL){
    sum=sum+f->gap;
    next=f->next;
    free(f);    
    f = next;
}
head = f;
if (f != NULL) {
    f->prev = NULL;
}
(Wasntme)

What is [m]head[/m]?? Is it maybe [m]p->asteroids[/m] ?? (Wondering)

Also what does the command [m]next=f->next;[/m] mean?? (Wondering)
 
  • #97
mathmari said:
What is [m]head[/m]?? Is it maybe [m]p->asteroids[/m] ?? (Wondering)

I don't know. You left out the context.
It's the pointer to the beginning of the list. (Dull)

Btw, is there also a Sentinel? (Wondering)
If so that also needs to be updated.
Also what does the command [m]next=f->next;[/m] mean?? (Wondering)

It means we save [m]f->next[/m] in an extra temporary variable (named [m]next[/m]), so that we still know what it is after we have freed [m]f[/m]. (Nerd)
Otherwise we might get a segmentation violation and we don't want that do we? (Devil)
 
  • #98
I like Serena said:
I don't know. You left out the context.
It's the pointer to the beginning of the list. (Dull)
I have to write a function for the destruction of the planetary system "solid".
With the destruction of the planetary system, the asteroids where the gap between this one and the object, is at least "gap" will also be destructed. (so, they have to be deleted) The asteroids for which the gap is greater are converted to free-floating planets consisting a new collection of free-floating planets (ffplan_t). This new collection should be added to the array of the free-floating planets of the star system to which the planetary system, that is destructed, belonged. The identifier "fp" of the new collection is the identifier of the planetary system that is destructed. The list of the free-floating planets that corresponds to the new collection should be sorted as for the field "as" of the asteroids that are contained. The destructed planetary system should be deleted from the list of planetary systems of the star system to which it belonged.

The function I have written is the following:

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

	
	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;
	
}
I like Serena said:
Btw, is there also a Sentinel? (Wondering)
If so that also needs to be updated.

There is no Sentinel in this list.
I like Serena said:
It means we save [m]f->next[/m] in an extra temporary variable (named [m]next[/m]), so that we still know what it is after we have freed [m]f[/m]. (Nerd)
Otherwise we might get a segmentation violation and we don't want that do we? (Devil)

Ahaa.. Ok! (Malthe)
 
  • #99
mathmari said:
The function I have written is the following:

There is no Sentinel in this list.

Ahaa.. Ok! (Malthe)

I see you have updated your program.
So what is it doing now? (Wondering)
Does it still give you a segmentation violation? (Sweating)
 
  • #100
I like Serena said:
I see you have updated your program.
So what is it doing now? (Wondering)
Does it still give you a segmentation violation? (Sweating)

Now I get the following:

[m]Program received signal SIGSEGV, Segmentation fault.
0x0040260c in destruction (solid=7620203, gap=5000) at stars.c:363
363 planf->as=f->as;[/m]

(Wondering)
 
  • #101
mathmari said:
Now I get the following:

[m]Program received signal SIGSEGV, Segmentation fault.
0x0040260c in destruction (solid=7620203, gap=5000) at stars.c:363
363 planf->as=f->as;[/m]

(Wondering)

Does that mean we've progressed again? (Wondering)

From [m]gdb[/m] you can get some more information by typing:

[m](gdb) p f[/m]

This will print the value of [m]f[/m].
It it is NULL that would confirm that [m]f[/m] is the problem.
Otherwise [m]planf[/m] might be the problem. (Nerd)
 
  • #102
I like Serena said:
Does that mean we've progressed again? (Wondering)

From [m]gdb[/m] you can get some more information by typing:

[m](gdb) p f[/m]

This will print the value of [m]f[/m].
It it is NULL that would confirm that [m]f[/m] is the problem.
Otherwise [m]planf[/m] might be the problem. (Nerd)

When I write [m](gdb p f[/m] I get the following:

[m]$1 = (planet_t *) 0x0[/m]

But why is it [m]NULL[/m] ?? (Wondering)
 
  • #103
mathmari said:
When I write [m](gdb) p f[/m] I get the following:

[m]$1 = (planet_t *) 0x0[/m]

But why is it [m]NULL[/m] ?? (Wondering)

How does [m]f[/m] get its value before the problematic line of code? (Wondering)
 
  • #104
I like Serena said:
How does [m]f[/m] get its value before the problematic line of code? (Wondering)

It is a pointer to the list [m]f=p->asteroids;[/m].
 
  • #105
mathmari said:
It is a pointer to the list [m]f=p->asteroids;[/m].

Then that list is empty for some reason. (Thinking)
 

Similar threads

Back
Top