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:
// 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:
// 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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- cin get() in C++ with Examples
- Pi(π) in C++ with Examples
- std::less in C++ with Examples
- std::is_default_constructible in C++ with Examples
- std::is_trivially_move_constructible in C++ with Examples
- std::is_copy_assignable in C++ with Examples
- std::add_volatile in C++ with Examples
- std::add_const in C++ with Examples
- std::equal_to in C++ with Examples
- std::is_literal_type in C++ with Examples
- std::is_nothrow_assignable in C++ with Examples
- std::is_trivially_constructible in C++ with Examples
- std::is_trivially_destructible in C++ with Examples
- std::bit_or in C++ with Examples
- std::greater in C++ with Examples
- std::is_move_constructible in C++ with Examples
- std::is_move_assignable C++ with Examples
- std::is_nothrow_default_constructible in C++ with Examples
- std::is_member_pointer in C++ with Examples
- std::is_nothrow_copy_assignable in C++ with Examples
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

