Open In App

std::get_temporary_buffer in C++

Last Updated : 09 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 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: initialize 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 
        uninitialized_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.

C++




// C++ 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: 

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. 

C++





Output

sorted characters 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.

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads