Title: Does unique pointer allocate memory on the heap or stack?
Tags: c++
Question: I'm a newcomer in C++. I tested a unique pointer on my PC: I thought it would be allocated on the heap, but it shows on the stack instead. This confused me. Does a unique pointer allocate memory on the heap or stack?
#include <memory&gt;
#include <stdio.h&gt;
void RawPointer()
int *raw = new int;// create a raw pointer on the heap
printf("raw address: %p\n", raw);
*raw = 1; // assign a value
delete raw; // delete the resource again
void UniquePointer()
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0unique_ptr<int&gt; unique(new int);// create a unique pointer on the stack
*unique = 2; // assign a value
// delete is not neccessary
printf("unique pointer address: %p\n", &amp;unique);
int main(){
RawPointer();
UniquePointer();
and it comes in shell:
raw address: 0x555cd7435e70
unique pointer address: 0x7ffcd17f38b0
thanks bros,
Comment: " but it shows on the stack instead" what exactly is "it" ? Its not clear how you come to your conclusion. There is a difference between the `unique_ptr` and the pointer it encapsulates, in case that is causing confusion

Comment: A unique pointer did not allocate anything. It only keeps a pointer to something, which can be on the heap or on the stack. And the unique pointer itself can be on stack or on heap. Allocating with new means heap allocation.

Comment: thanks, that means i could consider unique as a class, inside the int pointer point to the heap. am i right?


Here is another answer: In your example raw and unique are local variables, allocated on the stack, which you can check when printing the address of the variables address.
printf("raw pointer address: %p\n", (void*)&amp;raw);
printf("unique pointer address: %p\n", (void*)&amp;unique);
But the pointers raw and unique can point anywhere. If they point to an object allocated with new, then the pointer points to an object on the heap. Which you can see when printing the pointer value.
printf("raw pointer points to: %p\n", (void*)raw);
printf("unique pointer points to: %p\n", (void*)unique.get());
Please note that I've added a (void*) cast to the pointers I pass to printf because, if I remember correctly, %p requires a void*.

Here is another answer: What you are printing in your UniquePointer function is the address of the st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0unique_ptr class object. This class has an internal member that is the actual address of the allocated data.
You can access this actual address using the get() member function, as shown in the code below (which, when I tried, gave me the exact same address as for the raw pointer - though that won't always be the case):
void UniquePointer()
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0unique_ptr<int&gt; unique(new int);// create a unique pointer on the stack
*unique = 2; // assign a value
// delete is not neccessary
// printf("unique pointer address: %p\n", &amp;unique);
printf("unique pointer address: %p\n", unique.get());
Note: The %p format specifier expects a void * pointer as an argument; however, an implicit cast to a void pointer (from a pointer to another type) is well-defined behaviour - see here: void pointers: difference between C and C++.
Feel free to ask for further clarification and/or explanation.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:05/16/20', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<c++>', 'Title': 'Does unique pointer allocate memory on the heap or stack?', 'ViewCount': 988, 'id': 61834575, 'user_AboutMe': None, 'user_AccountId': 16377874.0, 'user_DisplayName': 'kit_J', 'user_Reputation': 53.0, 'user_Views': 5.0, 'user_id': 11829603.0}}