C++ std::错误的\u alloc运行时异常

C++ std::错误的\u alloc运行时异常,c++,memory-management,memory-leaks,dynamic-memory-allocation,bad-alloc,C++,Memory Management,Memory Leaks,Dynamic Memory Allocation,Bad Alloc,当循环到达第二轮时,我在服务器端得到std::bad_aloc异常。代码如下: void UDP_ProcessClient (int socketFileDescriptor, void *transmitBuffer, void *receiveBuffer, size_t transmitBufferSize, struct sockaddr *clientAddress, socklen_t clientAddressLength, char *workDir, size_t fileN

当循环到达第二轮时,我在服务器端得到std::bad_aloc异常。代码如下:

void UDP_ProcessClient (int socketFileDescriptor, void *transmitBuffer, void *receiveBuffer, size_t transmitBufferSize, struct sockaddr *clientAddress, socklen_t clientAddressLength, char *workDir, size_t fileNumber)
{
    size_t i;
    size_t bufferSize = 0;
    string resultName, imageName;

    for (i = 0; i < 6; i++)
    {
        /// Start communicating (read/write)
        /// Prepare buffers for communication
        cout << "[Server]: Receiving data from client..." << endl;
        UDP_ReceiveData(socketFileDescriptor, (uint32_t *) &bufferSize, sizeof(bufferSize), MSG_WAITALL, clientAddress, &clientAddressLength);      /// Get required data buffer size
        //cout << "[Server]: PHPDHPHPHFPSADHFI" << endl;
        receiveBuffer = new uint8_t[bufferSize];                                                                                                    /// Allocate memory
        clog << "Receiving file of size " << bufferSize << endl;
        UDP_ReceiveData(socketFileDescriptor, (int8_t *) receiveBuffer, bufferSize, MSG_WAITALL, clientAddress, &clientAddressLength);              /// Read data
        //cout << "SDFDSFDSFFADSFASDFSDVLALALL " << bufferSize << endl;

        cout << '\a';
        cout << "[Server]: Data received successful" << endl;

        /// Save image.bmp file
        resultName = workDir;
        imageName = "image_" + to_string(fileNumber) + ".jai";
        resultName += imageName;
        createImageFromBuffer((uint8_t *) receiveBuffer, bufferSize, resultName);

        /// Clear memory
        bufferSize = 0;
        delete[] (uint8_t *) receiveBuffer;
    }
}
服务器端的运行时异常:

terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
Aborted

一个可能的解释可能是缓冲区大小太大了。如果可能的话,试着调试代码,或者打印出bufferSize来查看它

如果无法分配请求的内存量,则会发生此情况。
bufferSize
的值是多少?看来是时候了解这应该是一个注释而不是答案了。多亏了stackoverflow策略,我还不能评论,因为这样做需要50个代表
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
Aborted