Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/memory/get temporary buffer"

From cppreference.com
< cpp‎ | memory
m (fmt)
(Added LWG issue #2072 DR (part 1/2).)
 
(6 intermediate revisions by 5 users not shown)
Line 3: Line 3:
 
{{dcl begin}}
 
{{dcl begin}}
 
{{dcl header|memory}}
 
{{dcl header|memory}}
{{dcl | until=c++11 |1=
+
{{dcl|until=c++11|1=
 
template< class T >
 
template< class T >
 
std::pair<T*, std::ptrdiff_t>
 
std::pair<T*, std::ptrdiff_t>
  get_temporary_buffer( std::ptrdiff_t count );
+
    get_temporary_buffer( std::ptrdiff_t count );
 
}}
 
}}
{{dcl| since=c++11 | deprecated=c++17 | removed=c++20 |1=
+
{{dcl|since=c++11|deprecated=c++17|removed=c++20|1=
 
template< class T >
 
template< class T >
 
std::pair<T*, std::ptrdiff_t>
 
std::pair<T*, std::ptrdiff_t>
Line 15: Line 15:
 
{{dcl end}}
 
{{dcl end}}
  
Allocates uninitialized contiguous storage, which should be sufficient to store up to {{tt|count}} adjacent objects of type {{tt|T}}. The request is non-binding and the implementation may allocate less or more than necessary to store {{tt|count}} adjacent objects.
+
If {{c|count}} is negative or zero, does nothing.
 +
 
 +
Otherwise, requests to allocate uninitialized contiguous storage for {{c|count}} adjacent objects of type {{tt|T}}. The request is non-binding, and the implementation may instead allocate the storage for any other number of (including zero) adjacent objects of type {{tt|T}}.
 +
 
 +
{{rrev|since=c++11|
 +
It is implementation-defined whether [[cpp/language#object#Alignment|over-aligned]] types are supported.
 +
}}
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | count | the desired number of objects}}
+
{{par|count|the desired number of objects}}
 
{{par end}}
 
{{par end}}
  
 
===Return value===
 
===Return value===
An {{lc|std::pair}} holding a pointer to the beginning of the allocated storage and the number of objects that fit in the storage that was actually allocated.
+
A {{lc|std::pair}}, the member {{tt|first}} is a pointer to the beginning of the allocated storage and the member {{tt|second}} is the number of objects that fit in the storage that was actually allocated.
  
If no memory could be allocated, or allocated storage is not enough to store a single element of type {{tt|T}}, the {{tt|first}} element of the result is a null pointer and the {{tt|second}} element is zero.
+
If {{c|1=count <= 0}} or allocated storage is not enough to store a single element of type {{tt|T}}, the member {{tt|first}} of the result is a null pointer and the member {{tt|second}} is zero.
 +
 
 +
===Notes===
 +
This API was originally designed with the intent of providing a more efficient implementation than the general-purpose {{c/core|operator new}}, but no such implementation was created and the API was deprecated and removed.
  
 
===Example===
 
===Example===
{{include| cpp/memory/example1}}
+
{{include|cpp/memory/example1}}
 +
 
 +
===Defect reports===
 +
{{dr list begin}}
 +
{{dr list item|wg=lwg|dr=425|std=C++98|before=the behavior when {{c|1=count <= 0}} was unclear|after=made clear}}
 +
{{dr list item|wg=lwg|dr=2072|std=C++98|before=it was not allowed to allocate insufficient memory|after=allowed}}
 +
{{dr list end}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/memory/dsc return_temporary_buffer}}
+
{{dsc inc|cpp/memory/dsc return_temporary_buffer}}
 +
{{dsc inc|cpp/memory/allocator_traits/dsc allocate_at_least}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 23:36, 17 August 2023

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
Dynamic memory management
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Allocators
Garbage collection support
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)



Uninitialized storage
(until C++20*)
get_temporary_buffer
(until C++20*)
 
Defined in header <memory>
template< class T >

std::pair<T*, std::ptrdiff_t>

    get_temporary_buffer( std::ptrdiff_t count );
(until C++11)
template< class T >

std::pair<T*, std::ptrdiff_t>

    get_temporary_buffer( std::ptrdiff_t count ) noexcept;
(since C++11)
(deprecated in C++17)
(removed in C++20)

If count is negative or zero, does nothing.

Otherwise, requests to allocate uninitialized contiguous storage for count adjacent objects of type T. The request is non-binding, and the implementation may instead allocate the storage for any other number of (including zero) adjacent objects of type T.

It is implementation-defined whether over-aligned types are supported.

(since C++11)

Contents

[edit] Parameters

count - the desired number of objects

[edit] Return value

A std::pair, the member first is a pointer to the beginning of the allocated storage and the member second is the number of objects that fit in the storage that was actually allocated.

If count <= 0 or allocated storage is not enough to store a single element of type T, the member first of the result is a null pointer and the member second is zero.

[edit] Notes

This API was originally designed with the intent of providing a more efficient implementation than the general-purpose operator new, but no such implementation was created and the API was deprecated and removed.

[edit] Example

#include <algorithm>
#include <iostream>
#include <iterator>
#include <memory>
#include <string>
 
int main()
{
    const std::string s[] = {"string", "1", "test", "..."};
    const auto p = std::get_temporary_buffer<std::string>(4);
    // requires that p.first is passed to return_temporary_buffer
    // (beware of early exit points and exceptions), or better use:
    std::unique_ptr<std::string, void(*)(std::string*)> on_exit(p.first,
    [](std::string* p)
    {
        std::cout << "returning temporary buffer...\n";
        std::return_temporary_buffer(p);
    });
 
    std::copy(s, s + p.second,
              std::raw_storage_iterator<std::string*, std::string>(p.first));
    // has same effect as: std::uninitialized_copy(s, s + p.second, p.first);
    // requires that each string in p is individually destroyed
    // (beware of early exit points and exceptions)
 
    std::copy(p.first, p.first + p.second,
              std::ostream_iterator<std::string>{std::cout, "\n"});
 
    std::for_each(p.first, p.first + p.second, [](std::string& e)
    {
        e.~basic_string<char>();
    }); // same as: std::destroy(p.first, p.first + p.second);
 
    // manually reclaim memory if unique_ptr-like technique is not used:
    // std::return_temporary_buffer(p.first);
}

Output:

string
1
test
...
returning temporary buffer...

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
LWG 425 C++98 the behavior when count <= 0 was unclear made clear
LWG 2072 C++98 it was not allowed to allocate insufficient memory allowed

[edit] See also

(deprecated in C++17)(removed in C++20)
frees uninitialized storage
(function template) [edit]
[static] (C++23)
allocates storage at least as large as the requested size via an allocator
(public static member function of std::allocator_traits<Alloc>) [edit]