I'm developing a client/server chat using C for a University project.
Everything is going fine, but i cannot fix this problem.
I created a struct to store the sockets of the two clients who have to communicate:
typedef struct pairing_Clients{
int client1fd;
int client2fd;
} pairedClients;
When i use it in a function that manages the communication between two clients, it gives me segfault. I tried with the following lines:
pairedClients *clients;
void client_handler(void *p_client) {
pairedClients *clients = (pairedClients *)malloc(sizeof(pairedClients));
clients->client1fd = 15;
}
It seems strange to me. I don't see any big problem. Any help?
When allocating you are creating a new variable and global clients
left uninitialized. Since the context is not clear, I guess that clients->client1fd = 15;
line is not in the same scope with the allocation line.
Just change the malloc
line to:
clients = (pairedClients *)malloc(sizeof(pairedClients));
Also you should check the return value of malloc
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.