我应该出于什么目的使用 std::get_temporary_buffer
?标准规定如下:
获取一个指向足以存储最多 n 个相邻 T 对象的存储的指针。
我认为缓冲区将在堆栈上分配,但事实并非如此。根据 C++ 标准,该缓冲区实际上不是临时的。与也不构造对象的全局函数 ::operator new
相比,该函数有什么优势。我认为以下陈述是等价的,对吗?
int* x;
x = std::get_temporary_buffer<int>( 10 ).first;
x = static_cast<int*>( ::operator new( 10*sizeof(int) ) );
这个函数只是为了语法糖而存在吗?为什么名字里有temporary
?
Dr. 中建议了一种用例。 Dobb's Journal,1996 年 7 月 1 日 用于实现算法:
如果无法分配缓冲区,或者缓冲区小于请求的缓冲区,算法仍然可以正常工作,只是速度会变慢。
For what purpose I should use std::get_temporary_buffer
? Standard says the following:
Obtains a pointer to storage sufficient to store up to n adjacent T objects.
I thought that the buffer will be allocated on the stack, but that is not true. According to the C++ Standard this buffer is actually not temporary. What advantages does this function have over the global function ::operator new
, which doesn't construct the objects either. Am I right that the following statements are equivalent?
int* x;
x = std::get_temporary_buffer<int>( 10 ).first;
x = static_cast<int*>( ::operator new( 10*sizeof(int) ) );
Does this function only exist for syntax sugar? Why is there temporary
in its name?
One use case was suggested in the Dr. Dobb's Journal, July 01, 1996 for implementing algorithms:
If no buffer can be allocated, or if it is smaller than requested, the algorithm still works correctly, It merely slows down.
发布评论
评论(6)
Stroustrup 在“C++ 编程语言”(§19.4.4,SE)中说道:
他还首先介绍了这两个函数:
...但似乎没有在任何地方提供临时或长期的定义。
轶事 /com/0321942043" rel="noreferrer">“从数学到通用编程” 提到 Stepanov 在原始 STL 设计中提供了一个虚假的占位符实现,但是:
Stroustrup says in "The C++ Programming Language" (§19.4.4, SE):
He also starts the introduction to the two functions with:
... but doesn't seem to provide a definition of temporary or longer-term anywhere.
An anecdote in "From Mathematics to Generic Programming" mentions that Stepanov provided a bogus placeholder implementation in the original STL design, however:
微软的标准库人员说了以下内容(此处):
Microsoft's standard library guy says the following (here):
该标准表示它为最多
n
个元素分配存储空间。换句话说,您的示例可能返回一个足以容纳 5 个对象的缓冲区。
不过,似乎很难想象一个好的用例。也许如果您正在一个内存非常有限的平台上工作,那么这是获取“尽可能多的内存”的便捷方法。
但在这样一个受限的平台上,我想你会尽可能绕过内存分配器,并使用内存池或你可以完全控制的东西。
The standard says it allocates storage for up to
n
elements.In other words, your example might return a buffer big enough for 5 objects only.
It does seem pretty difficult to imagine a good use case for this though. Perhaps if you're working on a very memory-constrained platform, it's a convenient way to get "as much memory as possible".
But on such a constrained platform, I'd imagine you'd bypass the memory allocator as much as possible, and use a memory pool or something you have full control over.
函数< /a> 在 C++17 中已弃用,因此现在正确的答案是“没有目的,请勿使用它”。
The function is deprecated in C++17, so the correct answer is now "for no purpose, do not use it".
响应可能少于请求。
base 的实际大小必须大于或等于require。
respond may be less than request.
the actual size of base must greater or equal to require.
也许(只是猜测)它与内存碎片有关。如果您大量地不断分配和释放临时内存,但每次这样做时,您都会在分配临时内存之后但在释放它之前分配一些长期预期内存,那么您最终可能会得到一个碎片堆(我猜)。
因此 get_temporary_buffer 可能旨在成为一个比您需要的内存块更大的内存块,该内存块被分配一次(也许有许多块准备好接受多个请求),并且每次您需要内存时,您只需获取其中一个大块。这样内存就不会碎片化。
Perhaps (just a guess) it has something to do with memory fragmentation. If you heavily keep allocating and deallocating temporal memory, but each time you do it you allocate some long-term intended memory after allocating the temp but before deallocating it, you may end up with a fragmented heap (I guess).
So the get_temporary_buffer could be intended to be a bigger-than-you-would-need chunk of memory that is allocated once (perhaps there are many chunks ready for accepting multiple requests), and each time you need memory you just get one of the chunks. So the memory doesn't get fragmented.