其他分享
首页 > 其他分享> > c – boost :: make_shared值是初始化还是默认初始化数组?

c – boost :: make_shared值是初始化还是默认初始化数组?

作者:互联网

我需要不断地将unsigned char的共享数组作为原始缓冲区分配,以保存来自TCP流的数据,然后将其传递给其他几个线程进行处理.我的问题是,boost :: make_shared< T []>(std :: size_t)值是否初始化或默认初始化基础数组?由于频率很高(每秒约十次),前者的开销太大.我尝试查看源代码,但是有太多的帮助程序类可以清楚地了解它在底层的作用.

解决方法:

docs

Effects: Allocates memory suitable for an array of type T and size size and constructs an array of objects in it via the placement new expression new(pointer) T() or new(pointer) T(args...). allocate_shared uses a copy of allocator to allocate memory. If an exception is thrown, has no effect.

这是执行数组的值初始化,因为它一次初始化每个元素.

Boost还为数组提供make_shared_noinit版本的make_shared函数,该函数不执行数组的初始化.这可能更适合您的需求.

标签:c,smart-pointers,boost
来源: https://codeday.me/bug/20190830/1771141.html