Again, Segfaults....

Good morning.

Again I'm here because of a segfault in my program. I don't know where it comes from, because it worked correctly before i've had my g++ updated...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
int zonencalc(player_data spieler[]){
	struct player_data *my_data;
	my_data = (struct player_data *) spieler;

	/*
		Spielerinfo
	*/
	int frische=my_data->frische;
	int kondition=my_data->kondition;
	int moral=my_data->moral;
	int form=my_data->form;
	int pos=my_data->pos;
	int team=my_data->team;
	int playerid=my_data->pid;
	int skill=my_data->skill;
	int lastfrische=my_data->lastfrische;
	
	printf("pid: %d\n team: %d\n",playerid,team);
	// Zonen data für Team vorbereiten
	struct zonen_data *zonen_data_now;
	zonen_data_now = (struct zonen_data *) &zonen[team];
	int t_z1=zonen_data_now->zone1;
	int t_z2=zonen_data_now->zone2;
	int t_z3=zonen_data_now->zone3;
	int t_z4=zonen_data_now->zone4;
	int t_z5=zonen_data_now->zone5;
	int t_z6=zonen_data_now->zone6;
	int t_z7=zonen_data_now->zone7;
	int t_z8=zonen_data_now->zone8;
	int t_z9=zonen_data_now->zone9;
....

And here's my terminal (+gdb) output:


1|Byron (12492)|(1)[991][173 cm][Frische: 0][Schnelligkeit: 31]
[New Thread 0xb7a0db90 (LWP 16130)]
Calczonen 991,12492
pid: 12492
team: 991

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0xb7a0db90 (LWP 16130)]
0x08049be8 in zonencalc (spieler=0x805a400) at engine.cc:4267
4267 int t_z1=zonen_data_now->zone1;
(gdb) print 0x08049be8
$2 = 134519784
(gdb) print 0x805a400
$3 = 134587392

So what's wrong?

Thank you

Marco
Well, I don't see the error right away, but...
(a) is the "spieler[]" const in this function, which is what I would assume? If so, why isn't it defined as such?
(b) Is the argument a pointer as in "pointer to one player_data" or is it a C (built-in) array? If the first, use player_data*, if the second, use a vector
(c) You do know that "struct" in the declaration is optional in C++ and rather obfuscating?
(d) You also know that C-style-casts belong to C, not C++? I don't see why you need to cast here anyways (line 3).
(e) Yeah, and in this line I see why your spieler[] in the argumentlist is *not* const. You copy the pointer. Is that what you want to do? I doubt it. So if you want the spieler[] to be const, again, declare it as such. That would have (supposing I am right) thrown a compiler-error, which is much better than a runtime-error.
(f) printf and the next c-style-cast don't belong here. They are error-prone, hard to understand, hard to debug, and hard to write correctly.
(g) Let us see the output of a 'bt' in gdb, would you?

(gdb) bt
#0 0x08049be8 in zonencalc (spieler=0x805a400) at engine.cc:4267
#1 0x08050cfd in poschecks (threadarg=0x805a400) at engine.cc:2328
#2 0xb7a744fb in start_thread () from /lib/i686/cmov/libpthread.so.0
#3 0xb7b5dd7e in clone () from /lib/i686/cmov/libc.so.6


engine.cc:2328 (poschecks) is calling zonencalc and handing over spieler[]

line 4267 is
int t_z1=zonen_data_now->zone1;

1
2
3
4
5
6
7
8
9
10
11
12
struct zonen_data{
	int zone1;
	int zone2;
	int zone3;
	int zone4;
	int zone5;
	int zone6;
	int zone7;
	int zone8;
	int zone9;
};
zonen_data zonen[NUM_THREADS4];


That would have (supposing I am right) thrown a compiler-error, which is much better than a runtime-error.

Unfortunately it's not about "spieler[]" it's about the "zonen" stuff...
"spieler[]" is working like I want it to, even if my code could look better.
I would say that &zonen[team] is calculating a bad address.
Is the value of the team variable valid? It may be trying to access outside
the zonen array.
Put a breakpoint on that line (21) and see what the value of team is before the line is executed.
Alright, it seems to be what you said. I changed the value of "team" and had before the values 1 and 2 (Num_THreads4 is 2). Now i've got 991...

Thanks, now I know what was wrong :)
Topic archived. No new replies allowed.