简体   繁体   English

数组指针 + 整数 (C++)

[英]Pointer of Array + Integer (C++)

I am not so familar with C++, so I want to ask, what the following code will do (I have it in an existing C++ project):我对C++不是很熟悉,所以想问一下,下面的代码会做什么(我在一个现有的C++项目中有它):

1: char* buf;
2: *buf = 0;
3: int readBytes = tcpSocket->read(buf, 10);
4: buf += readBytes;

Explanation is very simple, from a TCP Socket should be read 10 bytes and the read bytes are stored in the char* Buffer "buf".解释很简单,从一个 TCP Socket 应该读取 10 个字节并且读取的字节存储在 char* Buffer "buf" 中。 The return value is the number of read bytes.返回值是读取的字节数。

But why I need line 4?但是为什么我需要第 4 行? Or better what is line 4 doing?或者更好的是第 4 行在做什么? In my understanding it is destroying my "buf" result, isn't it?据我所知,它正在破坏我的“buf”结果,不是吗?

I hope someone can help me, and maybe explaining me, why I need this line 4.我希望有人可以帮助我,也许可以解释我,为什么我需要第 4 行。

BR ThW BR ThW

TCP is a streaming protocol, that means there are no message boundaries in the data you receive. TCP 是一种协议,这意味着您接收的数据中没有消息边界。 Therefore one might not get all the data one asks for in a single receive call, but have to loop and and read multiple times.因此,一个人可能无法在单个接收调用中获得所需的所有数据,而是必须多次循环和读取。

By doing eg buf += readBytes you advance the pointer buf by readBytes elements, so next time you receive the data will be written where the last receive call left of.通过执行例如buf += readBytes您将指针buf前进了readBytes元素,因此下次您接收数据时将写入最后一个接收调用左侧的位置。

There are some things that you need to fix through, besides you using and dereferencing an uninitialized pointer, and that is that you can't read a fixed number of bytes every iteration in the loop, you need to decrease the amount of data to receive by readBytes as well.除了使用和取消引用未初始化的指针之外,您还需要解决一些问题,那就是您无法在循环中的每次迭代中读取固定数量的字节,您需要减少要接收的数据量通过readBytes也是如此。 You also need to exit the loop once you read all the data.读取所有数据后,您还需要退出循环。

Lets put all this together into a nice function which will always read the requested amount of data, as an example:让我们将所有这些放在一个很好的函数中,该函数将始终读取请求的数据量,例如:

// tcpSocket is the socket to receive data from (I don't know the actual type)
// buffer is the destination buffer, where the received data should be written
// len is the number of bytes to receive
// Pre-condition: buffer must point to memory of at least len bytes
bool read_data(TCPSocketType* tcpSocket, char* buffer, size_t len)
{
    // Loop while there are still bytes to read
    while (len > 0)
    {
        ssize_t readBytes = tcpSocket->read(buffer, len);
        if (readBytes <= 0)
        {
            // There was an error (readBytes < 0)
            // Or the connection was closed (readBytes == 0)
            return false;
        }

        buffer += readBytes;  // Next position to write data into
        len -= readBytes;  // We don't need to read as much now
    }

    return true;  // Now we have read all of the data
}

Line 4 advances the pointer by the bytes you read, so that eg on the next call you don't overwrite your previous data.第 4 行通过您读取的字节推进指针,以便例如在下一次调用时您不会覆盖以前的数据。

As is stated in the comments line 2 is undefined behavior, because you didn't set your buf pointer to allocated memory.正如评论第 2 行所述,是未定义的行为,因为您没有将 buf 指针设置为分配的内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM