Thread: Why create a wrapper fo getaddrinfo()

  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 08:50 PM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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.

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, 09:50 AM
  3. How to create a DLL wrapper.
    By MindWorX in forum C Programming
    Replies: 12
    Last Post: 11-09-2006, 02: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