Subj : Re: Segmentation fault when using malloc with threads To : comp.programming.threads From : Paul Pluzhnikov Date : Sun Feb 27 2005 07:58 am "Karthik007" writes: > I have pasted some code snippets from the server program. AFAICT, your main will start corrupting stack after 10th connection: cliNum is ever increasing, but you only have space for 10 clients: > pthread_t srvThread[10]; > struct control data[10]; > int cliNum = 0; .... > cliNum++; > data[cliNum] = data[0]; The statement above probably does not do what you expect. All clients receive garbage (uninitialized) data as their 'control'. .... > info = (struct control *)malloc(sizeof(struct control)); > bzero(info, sizeof(struct control)); Why not simply "info = calloc(1, sizeof(struct control));" ? > info = (struct control *) arg; A memory leak: you've just allocated info dynamically, and now you threw away that memory and replaced it with the (auto) "&data[cliNum]" allocated on the main thread stack. > free(info); And now you corrupt heap by free()ing un-allocated memory. > new client the malloc() call > fails giving a "Segmentation fault" error. In your snippets above the problem is obvious. For some additional hints, read this: http://groups-beta.google.com/group/gnu.gcc.help/msg/ea48e30f0c1a62a5 Cheers, -- In order to understand recursion you must first understand recursion. Remove /-nsp/ for email. .