我需要可复制缓冲区,尽可能轻(例如不是零初始化)? [英] I need copyable buffer, as light as possible (e.g. not zero initialized)?

查看:125
本文介绍了我需要可复制缓冲区,尽可能轻(例如不是零初始化)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要非常大的原始缓冲区(让我们说MB比KB更多)尽可能轻。我想让它保持数据在动态区域,不一定是可以成长的,我将设置施工的大小。

I need raw buffers of very big size (let say more MB than KB) as light as possible. I would like to have it keep the data in dynamic area, not necessarily it shall be grow-able, I will set the size at construction.

我想到了 std :: vector< unsigned char> 但是:

std::vector<unsigned char>  a(VERY_BIG_SIZE);

已将数据初始化为 0 - I不需要这个,我不想支付它...这是嵌入式系统已经高度使用的CPU / RAM,我想受益于分配但未使用的内存只被虚拟分配的事实(I意味着它需要一些地址空间,但它不映射到真实内存,除非使用)。

has the data initialized to 0 - I don't need this, I don't want to pay for it... This is embedded system already of high usage of CPU/RAM and I want to benefit from the fact that memory allocated but not used is only virtually allocated (I mean it takes some address space but it is not mapped to real memory unless used).

我还想到:

std::vector<unsigned char> a;
a.reserve(VERY_BIG_SIZE);
std::copy(someData..., a.begin());

它令人惊讶地工作作为预期(我猜这个UB不知何故) - 和一个整个内存没有初始化,但正如你已经注意到,我不能复制一个到其他向量,因为 begin()== a.end()) ....

it surprisingly works as expected (I guess this UB somehow) - and the a whole memory is not initialized, but as you already noticed, I cannot copy a to other vector because (a.begin()== a.end())....

解释为什么我没有做 push_back(...)/ insert(a.end(),...)/ assign(...)方法。

I need to explain somehow why I did not do push_back(...)/insert(a.end(), ...)/assign(...) in my second approach.

我已经有了具有常量大小的模板类Buffer:

I already have template class Buffer with constant size:

template <size_t SIZE>
class Buffer {
public: 
// this "dirty" solution works, but I would need to add copy stuff by myself...
  Buffer()  { data.reserve(SIZE); }
// this "correct" solution is too heavy:
  Buffer() : data(SIZE) { }

  unsigned char* data() const { return &data[0]; }
private:
  std::vector<unsigned char> data;
  // unsigned char data[SIZE]; // this is not an option because it is not dynamic memory
}; 

有什么我可以放在 Buffer 将会处理内存管理,它将是可复制和不初始化...和内存将动态分配(像std :: Vector),这就是为什么 unsigned char data [SIZE] ; 不是一个选项。

Is there anything I can put in private part of Buffer that will take care of memory management and it will be copyable and not initialized... And memory would be dynamically allocated (like std::Vector does) that is why unsigned char data[SIZE]; is not an option.

任何想法?

推荐答案

打包到1个字节,并且其默认构造函数离开然后内容未初始化(即不做任何操作)应该工作。

Std vector of a struct that is packed to 1 byte and whose default constructor leaves then contents uninitialized (ie does nothing) should work.

注意,你可以做一个空矢量批量插入,初始化将来自传入数据。 Ie, vect.insert(vect.end(),random_access_iterator_begin,blah_end)可能有你想要的性能。

Note that you can do mass inserts into an empty vector, and the only initialization will be from the incoming data. Ie, vect.insert( vect.end(), random_access_iterator_begin, blah_end ) might have the performance you want.

这篇关于我需要可复制缓冲区,尽可能轻(例如不是零初始化)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆