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>
#include <stdio.h>
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()
{
std::unique_ptr<int> 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", &unique);
}
int main(){
RawPointer();
UniquePointer();
}
and it comes in shell:
raw address: 0x555cd7435e70
unique pointer address: 0x7ffcd17f38b0
thanks bros,