0

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:

  • Declared it as a global variable:
pairedClients *clients;
  • Then, doing this gives me the segfault:
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?

10
  • 1
    You don't have to cast the return value of malloc() in C Commented Aug 25, 2022 at 16:00
  • can you run valgrind? Commented Aug 25, 2022 at 16:04
  • I already tried valgrind, but i don't know why, when i do it, it uses another port, so i cannot run my client on that server Commented Aug 25, 2022 at 16:10
  • "this gives me the segfault:" You mean, when you run it in a debugger you get that line in that function shown as location for that error? These 2 lines look fine except for missing NULL check. If you inspect the variables, does clients contain a valid address? Commented Aug 25, 2022 at 16:19
  • 1
    Your code is incomplete; in particular, it seems to be missing a main() function and at least one #include. Please edit your code so it's a minimal reproducible example of your problem (including any necessary inputs, but preferably not needing any), then we can try to reproduce and solve it. You should also read How to Ask. Commented Aug 25, 2022 at 16:42

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

The two lines that gives me segfault are inside a function. However, i tried what you said, but unfortunately it still gives me segfault. Very strange.
Ok but don't forget that you are not initializing the global clients with that malloc line.
I get it now. I wanted to allocate it with malloc because i need that variable even in other functions. Is it better if i instantiate it with malloc when i declare it? Or in main?
You can't do it on the global scope in C I think so do it in the main. But in C++ you can.
If the 2 lines really are the location of the segfault, then it does not matter whether the gloval variable is initialized or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.