inet_ntoa liefert 0.0.0.0 wenn in cout mit weiterem verkettet



  • Hi!

    Ich kapiers nicht, bin ich jetzt schon ganz deppert?

    SOCKADDR_IN emptyAddr = { };
    
    SOCKADDR_IN testAddr = { };
    
    testAddr.sin_addr.s_addr = inet_addr("192.168.1.2");
    
    //cout << "DBG: " << inet_ntoa(emptyAddr.sin_addr) << " - " << inet_ntoa(testAddr.sin_addr) << endl;
    
    cout << "DBG: " << inet_ntoa(emptyAddr.sin_addr);
    cout << " - " << inet_ntoa(testAddr.sin_addr) << endl;
    

    Output wie erwartet:

    DBG: 0.0.0.0 - 192.168.1.2
    

    Doch mit dem auskommentierten Code:

    DBG: 0.0.0.0 - 0.0.0.0
    

    WTF? Warum ist dann testAddr 0.0.0.0? Warum muss ich den output aufteilen? Wie kann das für inet_ntoa() einen Unterschied machen?!

    😕



  • msdn schrieb:

    The string returned by inet_ntoa resides in memory that is allocated by Windows Sockets. The application should not make any assumptions about the way in which the memory is allocated. The string returned is guaranteed to be valid only until the next Windows Sockets function call is made within the same thread. Therefore, the data should be copied before another Windows Sockets call is made.



  • Achsooo, natürlich, danke! Wo kommt denn der Speicher her, hätt ich mich fragen müssen... manchmal streikt mein Hirn 😞


Anmelden zum Antworten