srgsanky
srgsanky

Reputation: 765

When is it safe to free sockaddr passed to bind?

Bind expects a pointer to struct sockaddr (https://man7.org/linux/man-pages/man2/bind.2.html). Is it safe to free this structure immediately after the call to bind or is it safe to free this only when the socket is closed?

If it is safe to free the structure immediately after the call to bind, does the kernel keep a copy of the address?

Upvotes: 1

Views: 126

Answers (1)

selbie
selbie

Reputation: 104569

You can free it immediately after the call.

Usually the sockaddr or sockaddr_in is declared on the stack. But if you're passing addresses around by pointer, you're ok free it immediately after bind returns. Same holds true for other socket functions like connect and sendto.

Upvotes: 2

Related Questions