C++ Null terminator

I’m reading in 300 characters here. I don’t understand why the null terminator ends at position 303 instead of 301?

char request_message[500];
int bytes_received = recv(accept(listening_socket, NULL, NULL), request_message, 300, 0) ;

if (bytes_received > 0) {

    int i = 0 ;

    while ( request_message[i] != '\0') {

        i++;

    }

    cout << i << endl; // The output is 303

}

Double check the documentation of recv. Does it guarantee that the bytes written to the buffer will end in \0? I’m guessing not. Instead, it returns the number of bytes received, and you can use that number to know where the string ends. Any bytes beyond that are probably uninitialized and undefined.

Okay. But where does the null terminator come from then? Typically, ‘\0’ is automatically included at the end of the chararray when you assign a string to it. But in this case it is automatically placed at the end + 3(it may even vary) after i recv() I find it after I have recv(), but the documentation mentions nothing if it includes null terminator or not.

In C++, “undefined” means could be literally anything. For example, if I write int x; cout << x;, and I ask what will this print, the answer is… could be literally anything! The value of x was never initialized with a value, so its value is whatever bit pattern happened to be left behind in that part of the memory from whatever ran last. Similarly, your buffer starts out uninitialized, 500 characters worth of effectively random data, whatever bits that happened to be left behind in that block of memory from whatever ran last. recv writes to the first bytes_received number of characters, so that part of the buffer has a known and initialized value, but everything beyond bytes_received is unitialized, effectively random data.

Yes. But I mean, it does not seem like it’s completely random if there’s always a null terminator after I recv the buffer/. If I recv 200, 300 or 400 char there is still always a null terminator betwéen 1-3 positions after the received bytes.

Trying printing all 500 bytes of the buffer (even the \0’s) both before and after the call to recv to spot the differences.

Okay. So when I create “char request_message[500]” and then look for ‘\0’, it will be found on the array’s first position (0).

Then, I use recv(). 422 bytes have been received and ‘\0’ are found at location 424 this time.

I also tested this instead:
“char * request_message = new char [500]”, and then when I searched for ‘\0’, after i created the new char, it was found at position 2 instead of 0.

Then, I use recv () the same way as before, so 422 bytes recived again, but ‘\0’ are found at location 422 instead of earlier 424.

I have no idea what’s happening :slight_smile:

Random data is what’s happening.

When you create “char request_message[500]”, all 500 bytes are random. A \0 could be found at any arbitrary position or not be found at all.

When you call recv(), then all bytes past the number of bytes received are still random. A \0 could be found at any arbitrary position after bytes received or not be found at all.

It seems more like the program tries to find the right position on its own and sometimes it succeeds and sometimes it doesn’t.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.