F
So,
ich habe mal versucht deine Änderungen einzubauen, aber immernoch den gleichen Effekt. Ich kopiere mal den kompletten Code der Funktion, vielleicht findet jemand noch einen Fehler (und bestimmt noch ein paar Verberssungsvorschläge).
int add_client(int s, struct chat_client **clnt, int *size_clnt, struct chat_cookie *ck) {
int i;
struct chat_client *new_clnt;
struct chat_client *tmp=*clnt;
time_t t;
char welcome[] = "Willkommen im Chat<br><br>";
printf("In add_client(), size of client struct is %i\n", *size_clnt);
time(&t);
/* Search next free element */
for (i = 0; i < *size_clnt; i++)
{
printf("Such bei ID %i: %i\n", i, tmp[i].id);
if (tmp[i].id != 0)
continue;
tmp[i].id = i + 1;
tmp[i].socket_id = s;
strcpy(tmp[i].cookie.cookie, ck->cookie);
strcpy(tmp[i].cookie.nick, ck->nick);
tmp[i].cookie.c_time = ck->c_time;
tmp[i].keep_alive = t;
break;
}
/* Reached we the last element? */
if (i > MAX_CLIENTS)
return -1;
/*
We need code the resize the clients array
if there are less than 3 free elements.
*/
if ((i + 3) > *size_clnt)
{
new_clnt = (struct chat_client*)realloc(tmp, (*size_clnt + INIT_CLIENTS) * sizeof(struct chat_client));
if (new_clnt == 0)
return -1;
*size_clnt += INIT_CLIENTS;
tmp = new_clnt;
*clnt = tmp;
}
write(s, welcome, strlen(welcome));
return i;
}
Aufgerufen wird die Funktion mit
clnt_id = add_client(c, &clnt, size_clnt, &c_lst[ck_id]);
c ist das Ergebnis eines socket_accept, clnt ist das besagte struct "chat client", size_clnt ist ein Pointer auf ein INT und c_lst ist ein Array eines anderen structs.
Beim 29. Aufruf von add_client() gibt es folgende Ausgabe
In add_client(), size of client struct is 32
Such bei ID 0: 1
Such bei ID 1: 2
Such bei ID 2: 3
Such bei ID 3: 4
Such bei ID 4: 5
Such bei ID 5: 6
Such bei ID 6: 7
Such bei ID 7: 8
Such bei ID 8: 9
Such bei ID 9: 10
Such bei ID 10: 11
Such bei ID 11: 12
Such bei ID 12: 13
Such bei ID 13: 14
Such bei ID 14: 15
Such bei ID 15: 16
Such bei ID 16: 17
Such bei ID 17: 18
Such bei ID 18: 19
Such bei ID 19: 20
Such bei ID 20: 21
Such bei ID 21: 22
Such bei ID 22: 23
Such bei ID 23: 24
Such bei ID 24: 25
Such bei ID 25: 26
Such bei ID 26: 27
Such bei ID 27: 28
Such bei ID 28: 29
Such bei ID 29: 30
Such bei ID 30: 0
Beim 30. Aufruf kommt dann
In add_client(), size of client struct is 64
Such bei ID 0: 1
Such bei ID 1: 1628570492
Vielleicht ist ja folgender Hinweis nützlich: Ich arbeite unter cygwin mit "gcc 3.3.3 (cygwin special)".