Why create a wrapper fo getaddrinfo()

This is a discussion on Why create a wrapper fo getaddrinfo() within the Linux Programming forums, part of the Platform Specific Boards category; Why do something like Code: struct addrinto * host_serv(const char *host, const char *serv, int family, int socktype) { int ...

  1. #1
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329

    Why create a wrapper fo getaddrinfo()

    Why do something like

    Code:
    struct addrinto *
    host_serv(const char *host, const char *serv, int family, int socktype)
    {
         int n;
         struct addrinfo hints, *res;
         bzero(&hints, sizeof(struct addrinfo));
         hints.ai_flags = AI_CANONNAME;
         hints.ai_famility = family;
         hints.ai_socktype = socktype;
    
         if( n =  getaddrinfo(host, serv, &hints, &res))  !=0)
              return (NULL);
         
        return (res);
    }
    And then call the function in main() like
    Code:
    struct addrinfo *ai;
    
    ai = Host_serv(host, NULL, 0, 0);
    Why not just call getaddrinfo() directly in main() like
    Code:
    struct addrinfo hints, *res;
    bzero(&hints, sizeof(struct addrinfo));
    hints.ai_flags = AI_CANONNAME;
    hints.ai_famility = family;
    hints.ai_socktype = socktype;
    
    if( n =  getaddrinfo(host, serv, &hints, &res) !=0)
         return (NULL);
    Last edited by Overworked_PhD; 11-09-2007 at 07:50 PM.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,789
    If I ask myself that question, the answer would be: convenience.
    I'd rather call an easy function with a few params and get the data I require than fill out a troublesome struct and pass along.
    It also makes the code cleaner and more structurized, if you ask me.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,672
    > Why not just call getaddrinfo() directly in main() like
    Why write functions at all?

    The point would be to keep main from becoming a massive bloated function which is impossible to maintain.

    Even if it has only a dozen lines, and you only ever call it once from just one place, it's still an improvement.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't create child windows
    By OnionKnight in forum Windows Programming
    Replies: 4
    Last Post: 04-10-2011, 04:13 PM
  2. Create new combo boxes based on items selected in preview combo box
    By RealityFusion in forum Windows Programming
    Replies: 2
    Last Post: 01-10-2007, 08:50 AM
  3. How to create a DLL wrapper.
    By MindWorX in forum C Programming
    Replies: 12
    Last Post: 11-09-2006, 01:52 PM
  4. Need help to compile a GTK+ code with C++ sql wrapper
    By kingsz1 in forum C++ Programming
    Replies: 2
    Last Post: 10-12-2006, 06:17 PM
  5. Create a file from c++ program
    By Dan17 in forum C++ Programming
    Replies: 2
    Last Post: 05-08-2006, 04:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21