Thread: IP Address Control Text Freeing?

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    28

    IP Address Control Text Freeing?

    Okay, I get the string version of an IP address ("." delimitted) using this:

    Code:
    in_addr iaIP;
    iaIP.S_un.S_un_b.s_b1 = (u_char)FIRST_IPADDRESS(dwIPVal);
    iaIP.S_un.S_un_b.s_b2 = (u_char)SECOND_IPADDRESS(dwIPVal);
    iaIP.S_un.S_un_b.s_b3 = (u_char)THIRD_IPADDRESS(dwIPVal);
    iaIP.S_un.S_un_b.s_b4 = (u_char)FOURTH_IPADDRESS(dwIPVal);
    
    char *ip = inet_ntoa(iaIP);
    With dwIPVal correctly filled in. I successfully get the ip in "ip" but I have a simple question: Do I have to free() it? Its a pointer to a char array and I dont see how itd get freed otherwise...



    EDIT: Actually... I don't even need the ip variable. But that's okay, I'd like to know if I have to free it anway
    Last edited by MitchellH; 05-22-2005 at 11:14 PM.

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    >> Do I have to free() it? <<

    No, inet_ntoa returns a pointer to a static buffer. A static variable is similar to a global, in that there is only one copy and its lifetime is the duration of the program. This string is valid until you next call inet_ntoa or another winsock function. After that it may be overwritten. If you want to keep the string longer than that you should copy it to your own buffer.
    Last edited by anonytmouse; 05-22-2005 at 11:18 PM.

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    28
    Thank you That answers my question perfectly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I thought pointers were pointers...
    By keira in forum C Programming
    Replies: 19
    Last Post: 08-15-2007, 11:48 PM
  2. How to get data from an IP address control box - VIS C++ 6
    By gazsux in forum Windows Programming
    Replies: 1
    Last Post: 03-17-2005, 11:19 AM
  3. Replies: 2
    Last Post: 09-05-2002, 10:12 AM
  4. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM
  5. MSN Vital Information
    By iain in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 09-22-2001, 08:55 PM