0
我有通過串行發送命令的功能,然後接收未知大小的響應。使用分配內存在函數返回用戶
(ioctl(fd_, FIONREAD, &bytes_in_buffer);
我確定需要爲我的讀取分配多少內存。
//This code calls the function below
unsigned char CheckRefresh[] = {254, 124, 0};
unsigned char * response;
unsigned int size;
relay_board->SendCustomCommand(CheckRefresh, 3, &response, size);
ErrorCode SendCustomCommand(unsigned char * command, unsigned int command_size, unsigned char **response, unsigned int &response_size)
{
//Send the command
write(fd_, command, command_size);
// ... Omitting Polling Code to Get correct number of bytes ...
(ioctl(fd_, FIONREAD, &bytes_in_buffer);
//Now getting the response
response_size = (unsigned int)bytes_in_buffer;
(*response) = new unsigned char(response_size);
if(read(fd_, *response, response_size) < 0)
{
std::cout << "[ProXRSerial] SendCustomCommand: Read failed... -- Errno: " << errno << std::endl;
return Failed;
};
return Success;
}
我相信這是我破壞棧,我的下一個函數調用處斷裂
unsigned char * command = new unsigned char(3);
下列要求:
sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) -
__builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long)
(old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2
* (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) &&
((unsigned long)old_end & pagemask) == 0)' failed.
任何人都可以提供一些建議?我很茫然,我以爲經過雙指針一樣,讓我來爲用戶分配內存...
預先感謝您。
* facepalm *非常感謝。有趣的是,迄今爲止,我所有的其他代碼一直在工作,做同樣的事情,但是訪問和寫作的方式超出了它的界限......哦堆。 – Constantin 2012-04-05 22:30:53
'響應=新無符號字符[responze_size];' – jrok 2012-04-05 22:31:04
同樣適用於'無符號字符*命令=新無符號字符(3);',沒有? – jpm 2012-04-05 22:31:58