std::to_address in C++ with Examples Last Updated : 04 Apr, 2020 Comments Improve Suggest changes Like Article Like Report The std::to_address, introduced in C++20, is used to obtain the address represented by the specified pointer without forming a reference to the pointee. The existing std::addressof cannot do std::addressof(*ptr) because *ptr isn’t always an object. The std::to_address solves these issues for us. Syntax: template class Ptr constexpr auto to_address(const Ptr& p) noexcept; template class T constexpr T* to_address(T* p) noexcept; Parameters: This method accepts a parameter p which is the fancy or raw pointer whose address is to be found. Return value: This method returns the Raw pointer that represents the address of pointer p. Below examples demonstrate the use of std::address Example 1: CPP // C++ code to show // the use of std::address #include <iostream> #include <memory> using namespace std; // Allocate memory and return // the pointer to that memory template <class A> auto allocator_new(A& a) { auto p = a.allocate(1); try { allocator_traits<A>::construct( a, to_address(p)); } catch (...) { a.deallocate(p, 1); throw; } cout << "Pointer to Memory allocated: " << p << endl; return p; } // Delete memory using // pointer to that memory template <class A> void allocator_delete( A& a, typename allocator_traits<A>::pointer p) { allocator_traits<A>::destroy( a, to_address(p)); a.deallocate(p, 1); cout << "Pointer to Memory deleted: " << p << endl; } // Driver code int main() { allocator<int> a; auto p = allocator_new(a); allocator_delete(a, p); } Output: Pointer to Memory allocated: 0x1512c20 Pointer to Memory deleted: 0x1512c20 Example 2: CPP // C++ code to show // the use of std::address #include <iostream> #include <memory> using namespace std; int main() { // Make a unique pointer and // use to_address to get its address // from heap memory cout << "Using unique pointers\n\n"; auto ptr = make_unique<int>(15); cout << "Address of pointer to 15: " << to_address(ptr) << "\n"; auto ptr1 = make_unique<int>(17); cout << "Address of pointer to 17: " << to_address(ptr1) << "\n"; // Use to_address to get the // address of a dumb pointer // from stack memory cout << "\nUsing dumb pointers\n\n"; int i = 17; cout << "Address of pointer to 17: " << to_address(&i) << "\n"; int j = 18; cout << "Address of pointer to 18: " << to_address(&j) << "\n"; return 0; } Output: Using unique pointers Address of pointer to 15: 0x181ec30 Address of pointer to 17: 0x181ec50 Using dumb pointers Address of pointer to 17: 0x7fff6b454398 Address of pointer to 18: 0x7fff6b45439c Reference: https://en.cppreference.com/w/cpp/memory/to_address Comment More infoAdvertise with us Next Article std::to_address in C++ with Examples S soham1234 Follow Improve Article Tags : Programming Language C++ CPP-Functions Practice Tags : CPP Similar Reads Array of list in C++ with Examples What is an array? An array in any programming language is a data structure that is used to store elements or data items of similar data types at contiguous memory locations and elements can be accessed randomly using indices of an array. Arrays are efficient when we want to store a large number of e 5 min read std::allocator() in C++ with Examples Allocators are objects responsible for encapsulating memory management. std::allocator is used when you want to separate allocation and do construction in two steps. It is also used when separate destruction and deallocation is done in two steps. All the STL containers in C++ have a type parameter A 3 min read array data() in C++ STL with Examples The array::data() is a built-in function in C++ STL which returns an pointer pointing to the first element in the array object. Syntax: array_name.data() Parameters: The function does not accept any parameters. Return Value: The function returns an pointer. Below programs illustrate the above functi 2 min read std::string::front() in C++with Examples It is used to access the first character from the string. It returns a reference to the first character of the string. Unlike member string::begin, which returns an iterator to this same character, this function returns a direct reference. Syntax: string str ("GeeksforGeeks"); Accessing first charac 2 min read Working and Examples of bind () in C++ STL In C++, the std::bind function is a part of the Standard Template Library (STL) and it is used to bind a function or a member function to a specific object or value. It creates a new function object by "binding" a function or member function to a specific object or value, allowing it to be called wi 5 min read Like