-1

I'm using g++ 4.9.2 compiler and wrote the following code to try my first example with sockets.

char *buf = new char[1000];
int iResult;

int main(){    
   WSADATA wsaData;
    SOCKET ConnectSocket = INVALID_SOCKET;
    addrinfo hints, *result;
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;
    std::cout << WSAStartup(MAKEWORD(2,2), &wsaData) << std::endl;
    iResult = getaddrinfo("173.194.40.231", "80", &hints, &result);
    std::cout << iResult;
    ConnectSocket = socket(result -> ai_family, result -> ai_socktype, result -> ai_protocol); //1, HERE
    connect(ConnectSocket, result->ai_addr, result->ai_addrlen);
}

The issue, that I got Segmentation fault at 1. Because of getaddrinfo("173.194.40.231", "80", &hints, &result); returned 11003, therefore &result pointed to NULL. Why? I typed valid address and port.

It was google.com and we can open up it in browser. Couldn't someone help me out?

user3663882
  • 6,957
  • 10
  • 51
  • 92
  • What does your debugger say? Where are your `#include`s? What output do you get before the segfault (if any)? – Lightness Races in Orbit Mar 23 '15 at 17:33
  • 2
    `The issue, that I got Segmentation fault at 1` First thing you should do is never assume things will work. You're supposed to test your return values instead of going on assuming things are ok. You are also to call `WSAGetLastError` when this occurs. – PaulMcKenzie Mar 23 '15 at 17:35
  • @LightnessRacesinOrbit My debugger said what I was wrote in the post. What else it could say? – user3663882 Mar 23 '15 at 19:09
  • @LightnessRacesinOrbit BTW, I tried to initialize addirnfo explicitly as `addrinfo hints = addrinfo();` and it worked. But I thought the declaration `addrinfo hints` should cause default initialization (that's with `addrinfo()`). What's wrong? – user3663882 Mar 23 '15 at 19:16
  • @user3663882 - The `hints` variable is local. Local variables do not have a default initial value unless you explicitly give it one. So the parentheses default initializes the struct. – PaulMcKenzie Mar 23 '15 at 20:37
  • @user3663882: It tells you a stack trace, the values of variables, all sorts. Just "segmentation fault" is pretty much _zero information_. – Lightness Races in Orbit Mar 23 '15 at 20:45
  • @PaulMcKenzie: That's not true. The default initialisation of members of aggregates with automatic storage duration is a no-op, sure. But you imply that every local variable is just not initialised, which is not true. – Lightness Races in Orbit Mar 23 '15 at 20:46
  • @LightnessRacesinOrbit - In the "give it one" in the comment, I meant to also include types with a user-defined default constructor. – PaulMcKenzie Mar 23 '15 at 21:37
  • @LightnessRacesinOrbit If `hints` is not default initialized so what value it have after the declaration `addrinfo hints`? – user3663882 Mar 24 '15 at 08:23
  • @user3663882: Unspecified value. From the look of `getaddrinfo` that shouldn't matter, as it's strictly an "out" parameter. – Lightness Races in Orbit Mar 24 '15 at 11:49

1 Answers1

1
memset(&hints, 0, sizeof(hints));

getaddrinfo() tries to fill up with existing data provided. Garbage data is confusing the function.

Andrew Komiagin
  • 6,446
  • 1
  • 13
  • 23
  • This would also work: `addrinfo hints = addrinfo();` – PaulMcKenzie Mar 23 '15 at 17:46
  • @PaulMcKenzie Why should we ever do that? Does `addrinfo hints;` declaration already cause default initializationn? – user3663882 Mar 23 '15 at 19:34
  • @user3663882 No, the declaration alone (without parentheses) doesn't do that -- you wind up with junk, just as the answer says. Adding the parentheses default initializes the struct, which makes those members all 0. – PaulMcKenzie Mar 23 '15 at 20:35
  • @PaulMcKenzie Yes, it does. Please read carefully the chapter 8.5/12 of the C++ Final Working Draft. I cite the crucial part: If no initializer is specified for an object, the object is default-initialized. What would you say? I'm confused.... – user3663882 Mar 24 '15 at 07:59
  • @user3663882 Well how do you explain why your struct was junk-filled? If local variables (leaving out ones with a user-defined default constructor) were automatically initialized to 0, then we would have no questions on SO concerning these issues. http://stackoverflow.com/questions/1069621/are-members-of-a-c-struct-initialized-to-0-by-default http://stackoverflow.com/questions/3153725/typedef-struct-default-initialization and please see the answer with 12 upvotes here: http://stackoverflow.com/questions/16782103/initializing-default-values-in-a-struct – PaulMcKenzie Mar 24 '15 at 09:51
  • @PaulMcKenzie So, if the variable `hints` were a global variable it would be default initialized, am I right? But in the case of local variables the value is indeterminate. – user3663882 Mar 24 '15 at 10:22
  • @user3663882 Yes, if it were a global variable, then you would have default initialization. – PaulMcKenzie Mar 24 '15 at 17:05
  • @PaulMcKenzie Thank you very much. Why don't you provide your comments as an answer? I think it might be useful for someone else... – user3663882 Mar 24 '15 at 19:53