Subj : Re: Segmentation fault when using malloc with threads To : comp.programming.threads From : Karthik007 Date : Sun Feb 27 2005 11:42 am >your main will start corrupting stack after 10th >connection: >cliNum is ever increasing, but you only have >space for 10 clients: Yes, sir what you said is rite this programs supports only 10 clients i have trimmed the code so the condition is missing. > 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'. No sir i have no problem with that(i have trimmed the code so you are unable to see the initilization of data[0] with data) coz i have connected some clients and it works perfectly and with structure instances you can assign the data in one instance to the other by just an equality sign (like s1 = s2). If there is anything different when using array of structure instances please let me know. >info = (struct control *)malloc(sizeof(struct control)); >bzero(info, sizeof(struct control)); >Why not simply "info = calloc(1, sizeof(struct control));" ? Yes, sir i'll defnitely take this into consideration. > 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. Yes, sir now i understand the problem. Thanks a lot for your valuable suggestion. Sir, if i use memcpy() function to copy the data pointed by arg ("&data[cliNum]") into the memory pointed by "info" then this problem can be solved i guess. Am i right in this regard ? Thanking you Karthik .