c++ - Cout gives no output after declaring an std::string variable -
i've written simple program returning hostname of ip address passed argument. program uses 2 functions: getaddrinfo() , getnameinfo(). i'm using linux mint, netbeans ide , g++ compiler. output alright, there no errors, when declare an
std::string str; then cout gives no output, nothing printed on screen. when comment out std::string declaration or remove it, statement
std::cout << "hostname: " << hostname << std::endl; prints returned hostnames successfully.
what may cause of such 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 comment out line, cout prints 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 declare std::string str variable, cout doesn't either print std::cout << "test" << std::endl; return 0; }
arguments host , serv pointers caller- allocated buffers (of size hostlen , servlen respectively) getnameinfo() places null-terminated strings containing host , service names respectively. http://man7.org/linux/man-pages/man3/getnameinfo.3.html
your pointers must allocated. fact commenting out line changes coincidence or strange side effect of optimization.
Comments
Post a Comment