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 return the pair of objects first and second. When memory is allocated the first contain the pointer to the first element in the block and the second contains the size . if the memory block is not allocated then in pair first contain null pointer and second contain zero.
Examples
- 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 Ootput : It contain 10 elements sorted array is 1, 2, 5, 8, 9, 10, 14, 17, 18, 37 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 <<
" "
;
for
(i = 0; i < 10; i++) {
cout << b[i] <<
" "
;
}
cout <<
" "
;
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);
cout <<
"sorted array :"
<< endl;
for
(i = 0; i < p.second; i++) {
sort(p.first, p.first + p.second);
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
- 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 it will print the contents in 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);
cout <<
"sorted characters are :"
<< endl;
for
(i = 0; i < p.second; i++) {
sort(p.first, p.first + p.second);
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.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
leave a comment
0 Comments