[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Question regarding the example of getaddrinfo() in the manual page.



Hello,

I have one question regarding the example of getaddrinfo() (getaddrinfo(3)).

The example is something like:

for (rp = result; rp != NULL; rp = rp->ai_next) {
    sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
    if (sfd == -1)
        continue;

    if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == 0)
        break;                  /* Success */

    close(sfd);
}

if (rp == NULL) {               /* No address succeeded */
    fprintf(stderr, "Could not bind\n");
    exit(EXIT_FAILURE);
}

freeaddrinfo(result);           /* No longer needed */

My question is: if socket() always fails or bind() always fails, the application would call(EXIT_FAILURE); without calling freeaddrinfo(result);

Though it is not that bad because the memory will be freed anyway, wouldn't it be better to call freeaddrinfo(result); before the exit?

Thank you


Reply to: