调用linearize后奇怪的boost循环缓冲行为
Strange boost circular buffer behaviour after calling linearize
我已经在我的boost::circular_buffer
上实现了一个提取方法,它只是将n
元素复制到destination
向量,然后从循环缓冲区中删除这n个元素(因此其内部指针被更新,标记可以再次写入的地方):
void CircularBuffer::extract(const unsigned int n, vector<complex<float>> *destination){
// Wait until the buffer is not empty
std::unique_lock<mutex> l(lock);
notEmpty.wait(l, [this, n](){
return (int)(circularBuffer.size() - n) >= 0;
});
// We must copy n elements from a linearized version of the buffer
memcpy(destination, circularBuffer.linearize(), n);
// Remove extracted elements from circular buffer
circularBuffer.erase(circularBuffer.begin(), circularBuffer.begin() + n); //SIGSEGV
// Not full any more
notFull.notify_one();
}
当调用erase
方法时,我得到一个分割错误。
我使用以下大小:
n = 9000
circularBuffer.size() = 9000 (at the moment when extract method is called)
circularBUffer.capacity() = 90000
但是,一旦执行memcpy
行,我猜是因为linearize
调用,一切都是混乱的,调试器显示:
circularBuffer.size() = 3238197033 (Hex: Hex:0xc102f729)
circularBUffer.capacity() = 18446744073434141805 (Hex:0xffffffffef95946d)
我可能不明白线性化方法是如何工作的,但它看起来很奇怪。
如果我继续,并且调用erase方法,则会引发分割错误并结束程序。如果我擦除的数据比缓冲区容量多,我可以理解,但事实并非如此。
帮忙吗?
您的记忆有误。您正在将数据复制到vector
对象本身的地址中,而不是将其复制到向量指向的位置。请确保在调用此函数之前调用vector::reserve
,以避免不必要的内存取消分配和分配。
#include <iterator>
void CircularBuffer::extract(const unsigned int n,
vector<complex<float>>& destination)
{
// Wait until the buffer is not empty
std::unique_lock<mutex> l(lock);
notEmpty.wait(l, [this, n](){
return (int)(circularBuffer.size() - n) >= 0;
});
auto cb_ptr = circularBuffer.linearize();
assert (cb_ptr);
std::copy(cb_ptr, cb_ptr + n, std::back_inserter(destination));
circularBuffer.erase(circularBuffer.begin(), circularBuffer.begin() + n);
// Not full any more
notFull.notify_one();
}
相关文章:
- C++ 和 Boost.Python - 如何将变量公开给 python 并在循环中更新它?
- 在原始循环上使用boost::irange的性能损失
- cpp 为什么 boost::线程内部的循环不遍历所有值并且推送相同的值?
- Boost.Process - 从具有休眠循环的进程读取
- 如何将前向声明与 boost::msm 一起使用以避免循环依赖?
- boost::asio::async_read 无限循环,接收数据为零字节
- Qt有循环缓冲吗?
- boost :: asio :: io_service async_write在做async_read时循环
- 使用boost ::几何::缓冲函数
- 如何使用Boost Graph库使用循环中的循环设置相同的边缘重量
- 运行 boost::asio 异步服务器以及命令循环
- 未定义的行为怪癖:在缓冲区外读取导致循环永远不会终止
- 调整 C++ std/boost iostream 以提供对内存块的循环写入
- BOOST :: ASIO带有主/工人线程 - 我可以在发布工作之前启动事件循环
- for_each算法在boost::multi_array上循环
- 调用linearize后奇怪的boost循环缓冲行为
- 如何在boost::循环缓冲区上使用boost::regex ?
- 如何判断 Boost 循环缓冲区的当前'write position',以便能够访问以前存储的值
- c++ Boost循环通过未知的ptree和列表属性值
- 访问字符串映射和Boost循环缓冲区的元素