Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用变量初始化缓冲区数组的长度_C++_C - Fatal编程技术网

C++ 使用变量初始化缓冲区数组的长度

C++ 使用变量初始化缓冲区数组的长度,c++,c,C++,C,我正在从套接字读取缓冲区字节,但我不知道如何使用长度信息初始化缓冲区数组 uint32_t len; int lengthbytes = 0; int databytes = 0; // receive the length info of an image data lengthbytes = recv(clientSocket, (char *)&len, sizeof(len), 0); // convert hexadecimal data to length in byte

我正在从套接字读取缓冲区字节,但我不知道如何使用长度信息初始化缓冲区数组

uint32_t len;
int lengthbytes = 0;
int databytes = 0;

// receive the length info of an image data
lengthbytes = recv(clientSocket, (char *)&len, sizeof(len), 0);

// convert hexadecimal data to length in bytes
len = ntohl(len);

// ????? how to initialize the buffer array with the length info ????
char buf[len];   -----> this is illegal in C  

// read the image data 
databytes = recv(clientSocket, buf, sizeof(buf), 0);

这在C99中有效,称为可变长度数组。如果不使用C99,请使用
malloc
分配数组(并将
buf
声明为
char*
)。

必须使用动态内存分配

char* buf = new char[len];
如果已使用
buf
,请不要忘记调用
delete
以释放内存

delete[] buf;

请通过
malloc
分配缓冲区,即
buf=malloc(sizeof(char)*len)

您可以使用new或malloc来完成。
完成后不要忘记删除缓冲区

当您声明
buf
时,您将声明一个可变长度数组。这在C(从C99标准)中是合法的,但是在C++中是非法的。在C++中,你可以使用<代码> STD::向量< /代码>:

std::vector<char> buf(len);

要在循环中使用向量,有两种选择:

  • 在循环外部声明变量,并在需要时使用和:

    std::vector<char> buf;
    
    // ...
    
    for (int i = 0; i < number_of_images; i++)
    {
        std::cout << "Fetching image #" << (i + 1) << '\n';
    
        // Get the image length
        size_t length = get_image_length();
    
        buf.clear();  // Clear the buffer
        buf.resize(length);  // Set the size to the image length
    
        // Receive the image
        databytes = recv(clientSocket, &buf[0], buf.size(), 0);
    }
    
    std::vector buf;
    // ...
    对于(int i=0;i<图像的数量;i++)
    {
    
    std::cout您可以使用
    std::vector
    ,然后将其
    data()
    用作数组缓冲区:

    #include <vector>
    std::vector<char> buf(len);
    databytes = recv(clientSocket, buf.data(), buf.size(), 0); // access underlying char array
    databytes = recv(clientSocket, &buf[0], buf.size(), 0);    // as above, C++03 version
    
    #包括
    std::载体buf(len);
    databytes=recv(clientSocket,buf.data(),buf.size(),0);//访问底层字符数组
    databytes=recv(clientSocket,&buf[0],buf.size(),0);//如上所述,C++03版本
    
    <代码> > p>我在C++中编写了一个名为 TunbFf 的类。 您可以在这里找到它:

    这两个文件是麻省理工学院授权的,所以你可以随意使用它

    如何使用这个类

    tempbuf buf(len);
    databytes = recv(clientSocket, buf.get(), buf.size(), 0); // if you want char* returned
    databytes = recv(clientSocket, buf.constchar(), buf.size(), 0); // if you want constchar* returned
    
    猜猜我为什么写这个类?你不需要删除或释放动态分配的内存,因为它是在类的析构函数中完成的


    为什么我没有使用
    std::auto_ptr
    ?因为据我所知,这只适用于非数组,因为它支持
    new X
    ,但不支持
    new X[10]

    如果我想使用while循环来接收许多图像,如何删除每个循环末尾的
    buf
    的内容?
    for (int i = 0; i < number_of_images; i++)
    {
        std::cout << "Fetching image #" << (i + 1) << '\n';
    
        // Get the image length
        size_t length = get_image_length();
    
        std::vector<char> buf(length);
    
        // Receive the image
        databytes = recv(clientSocket, &buf[0], buf.size(), 0);
    }
    
    #include <vector>
    std::vector<char> buf(len);
    databytes = recv(clientSocket, buf.data(), buf.size(), 0); // access underlying char array
    databytes = recv(clientSocket, &buf[0], buf.size(), 0);    // as above, C++03 version
    
    tempbuf buf(len);
    databytes = recv(clientSocket, buf.get(), buf.size(), 0); // if you want char* returned
    databytes = recv(clientSocket, buf.constchar(), buf.size(), 0); // if you want constchar* returned