Get a block of temporary memory. In C++ STL library, there is a function get_temporary_buffer which is mostly used to get a temporary block.
- This function take a size n and return the largest available buffer up to size n which can be fit into physical memory.
- This function is used to get a memory of temporary nature mostly used for the operation of an algorithm as some algorithms required extra space to perform correctly .
- Once the the memory block which is assigned is not needed anymore it shall be released by calling return_temporary_buffer .
Syntax:
pair(int*, ptrdiff_t) p = get_temporary_buffer(int)(required size)
Parameters:
- n: Number of elements of type T for which temporary memory is allocated.
- ptrdiff_t: it is an integral type.
Return: The function returns the first and second pair of objects. When memory is allocated the first contains the pointer to the first element in the block and the second contains the size. If the memory block is not allocated than the first pair contains null pointer and second contains zero.
Example 1:
To count the total even numbers in an array and print the sorted array using get_temporary_buffer
Input : 8, 9, 2, 1, 10, 14, 37, 18, 17, 5 Output : It contain 10 elements Sorted array is 1, 2, 5, 8, 9, 10, 14, 17, 18, 37 Explanation: Step 1: initiliaze the array b[] first, we find the even number elements in an array using for loop[0-n-1] if(a[i]%2==0){ c++;} print the count of even number. Step 2: use get_temporary buffer to allocate the block of memory pair(int*, ptrdiff_t) p=get_temporary_buffer(int)(required size) here required size is 10 Step 3: now copy the elements in the temporary buffer unitialized_copy(b, b+p.second, p.first); now using for loop [0 to p.second-1] sort the array using sort function sort(p.first, p.first+p.second) and finally print the sorted array.
// CPP code to demonstrate the get_temporary_buffer // to sort an array #include <iostream> #include <algorithm> #include <memory> using namespace std; void sorting( int b[], int n) { int i, c = 0; for (i = 0; i < n; i++) { if (b[i] % 2 == 0) { c++; } } cout << "The total even numbers are: " << c << endl; cout << "original array :" << " " ; cout << "\n" ; for (i = 0; i < 10; i++) { cout << b[i] << " " ; } cout << "\n" ; pair< int *, ptrdiff_t > p = get_temporary_buffer< int >(10); // copy the contents in temporary buffer with pair uninitialized_copy(b, b + p.second, p.first); sort(p.first, p.first + p.second); cout << "sorted array :" << endl; for (i = 0; i < p.second; i++) { cout << p.first[i] << " " ; } } // driver program to test above function int main() { int b[] = { 8, 9, 2, 1, 10, 14, 37, 18, 17, 5 }; int n = sizeof (b) / sizeof (b[0]); sorting(b, n); return 0; } |
Output:
The total even numbers are: 5 original array : 8 9 2 1 10 14 37 18 17 5 sorted array : 1 2 5 8 9 10 14 17 18 37
Example 2:
To sort the string alphabetically using get_temporary_buffer and return_temporary_buffer
Input : 'b', 'g', 'y', 'v', 'p' Output : b g p v y This will print the contents in an increasing order of alphabets.
// CPP code to sort the characters // alphabetically using std::get_temporary_buffer #include <iostream> #include <algorithm> #include <memory> #include <string.h> using namespace std; void sorting( char b[], int n) { int i; pair< char *, ptrdiff_t > p = get_temporary_buffer< char >(n); // copy the contents in temporary buffer with pair uninitialized_copy(b, b + p.second, p.first); // sort char array sort(p.first, p.first + p.second); cout << "sorted characters are :" << endl; for (i = 0; i < p.second; i++) { cout << p.first[i] << " " ; } // to release the temporary buffer return_temporary_buffer(p.first); } // driver program to test above function int main() { char str[] = { 'b' , 'g' , 'y' , 'v' , 'p' }; int c; c = strlen (str); sorting(str, c); return 0; } |
Output:
sorted charaters are : b g p v y
Application : Algorithms often required temporary space to perform correctly. It has very specialized purpose used internally by STL in algorithms like stable_partition, stable_sort and inplace_merge they use extra temporary memory to store intermediate results, and their run-time complexity is better if extra memory is available.
This article is contributed by Shivani Baghel. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.