Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6232839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:11:20+00:00 2026-05-24T10:11:20+00:00

I’m sure that it’s due to a silly mistake, but I have been debuging

  • 0

I’m sure that it’s due to a silly mistake, but I have been debuging during hours and I couldn’t solve the issue.

In my program, I have a function that returns the address of a given domain. It calls to getaddrinfo, copies the address to a new dinamically asigned variable, sets its pointer into a hostent struct and returns it. Nevertheless, the address returned wasn’t correct. After some debuging I realized that its value changes unexpectedly in some point of the code.

I made a sample program with the relevant part in this issue:

#include <iostream>
#include <string.h>
#include <string>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <stdlib.h>

using namespace std;
typedef unsigned long int longip_t;

int main(int argc, char** argv) {

    struct addrinfo hints, *res;
    struct hostent *final = new hostent;

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    getaddrinfo("automation.whatismyip.com", "80", &hints, &res);

    cout << inet_ntoa(((sockaddr_in*)res->ai_addr)->sin_addr) << endl;
    in_addr *addr = new in_addr;
    memcpy((void*)addr, (void*)(&((sockaddr_in*)res->ai_addr)->sin_addr), sizeof(in_addr));
    cout << inet_ntoa(*addr) << endl;

    final->h_addr_list = (char**)addr; 
    cout << inet_ntoa(*addr) << endl;
    final->h_addr = (char*)addr; // At this point the value of addr changes
    cout << inet_ntoa(*addr) << endl;
    cout << inet_ntoa(*(in_addr*)final->h_addr_list) << endl;
    exit(0);
}

The output I get:

72.233.89.195
72.233.89.195
72.233.89.195
48.116.218.9
48.116.218.9

The first three IPs are correct, but I get different values for the last two IPs every time I run the program. It looks that the value of addr changes after setting its pointer into the hostent struct.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T10:11:22+00:00Added an answer on May 24, 2026 at 10:11 am

    From hostent Structure on MSDN

    h_addr_list A NULL-terminated list of addresses for the host.
    Addresses are returned in network byte order. The macro h_addr is
    defined to be h_addr_list[0] for compatibility with older software.

    final->h_addr = (char*)addr;
    

    So the above line is really this

    final->h_addr_list[0] = (char*)addr;
    

    From final->h_addr_list = (char**)addr;, that line becomes this

    addr[0] = (char*)addr;
    

    Now, I believe you see the problem. 🙂

    [Update]

    The right way to set it is actually easy and automatic. You should call gethostbyaddr or gethostbyname, and they will return a pointer to hostent. This is well explained in MSDN. If you want to set hostent manually, which I don’t recommend, you have to create an array of pointers to addr.s_addr and set h_addr_list to the array. The array should include the last null element. I don’t see any reasons why you want to do it manually as OS does it for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a French site that I want to parse, but am running into
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.