Con*_*tin 0 c++ heap-corruption
我有一个函数,它通过串行发送命令,然后接收未知大小的响应.运用
(ioctl(fd_, FIONREAD, &bytes_in_buffer);
Run Code Online (Sandbox Code Playgroud)
我确定需要为阅读分配多少内存.
//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;
}
Run Code Online (Sandbox Code Playgroud)
我相信这会破坏我的堆栈,因为我的下一个函数调用会中断
unsigned char * command = new unsigned char(3);
Run Code Online (Sandbox Code Playgroud)
以下内容:
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.
Run Code Online (Sandbox Code Playgroud)
有人可以提供一些建议吗?我很茫然,我认为传递双指针会让我为用户分配内存......
先感谢您.
这条线
(*response) = new unsigned char(response_size);
Run Code Online (Sandbox Code Playgroud)
应该读
(*response) = new unsigned char[response_size];
Run Code Online (Sandbox Code Playgroud)
您的版本分配一个无符号字符,并使用该值进行初始化response_size.
| 归档时间: |
|
| 查看次数: |
101 次 |
| 最近记录: |