2

I've written a simple program returning the hostname of the IP address passed as an argument. The program uses two functions: getaddrinfo() and getnameinfo(). I'm using Linux Mint, Netbeans IDE and the G++ compiler. The output is alright, there are no errors, but when I declare an

std::string str;

then cout gives no output, nothing is printed on the screen. However when I comment out the std::string declaration or remove it, the statement

std::cout << "hostname: " << hostname << std::endl;

prints the returned hostnames successfully.

What may be the cause of such a strange error?

#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <iostream>
#include <string>

int main()
{
    struct addrinfo* result;
    struct addrinfo* res;
    int error;
    const char* host;
    // When I comment out this line, cout prints the hostnames succesfully.
    std::string str;

    error = getaddrinfo("127.0.0.1", NULL, NULL, &result);
    res = result;

    while (res != NULL)
    {
        char* hostname;
        error = getnameinfo(res->ai_addr, res->ai_addrlen, hostname, 1025, NULL, 0, 0);
        std::cout << "hostname: " << hostname << std::endl;
        res = res->ai_next;
    }

    freeaddrinfo(result);
    // When I declare an std::string str variable, this cout doesn't either print anything
    std::cout << "TEST" << std::endl;

    return 0;
}
2
  • 1
    I imagine hostname needs allocated memory. Commented Apr 5, 2013 at 21:24
  • 5
    C++ doesn't work that way. It has nothing to do with the string. You can't just wildly declare a char * and hope it points somewhere sensible. Look at this example for some inspiration. Commented Apr 5, 2013 at 21:25

2 Answers 2

3
   The arguments host and serv are pointers to caller-
   allocated buffers (of size hostlen and servlen respectively) into which
   getnameinfo() places null-terminated strings containing the host and
   service names respectively.

http://man7.org/linux/man-pages/man3/getnameinfo.3.html

Your pointers must be actually allocated. The fact that commenting out that line changes anything is probably a coincidence or a strange side effect of optimization.

Sign up to request clarification or add additional context in comments.

Comments

0

Thanks, it works now :). I'd like to find out when to use different ways to allocate memory. As far as I know the main difference between creating an object in the following way:

// Creating objects:
Test t1;
Test* t2 = new Test();
  1. The first object will be created in a heap and it will be automatically deleted when the function is finished running.
  2. The second object will be created in a stack and memory deallocation has to be done manually using the delete/delete[] operators?

So what else should I keep in mind when dealing with pointers? I guess I need to read a good book about computer architecture, as knowledge about memory and microprocessors will give profits :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.