+ 2

Dynamic Memory allocation with the new operator

I declare a pointer using the new operator just like in the example below: int *p = new int; Then I assign a value to that pointer: *p = 5; So far, so clear. My question has to do with the ampersand operator. To be specific, I post my code below: cout << *p << &p << p << endl; The first output will be 5, which represents the value that is stored in the address located in the heap, which in turn is contained as the value of pointer p stored as a local variable in the stack. Next, the "&p" will printout the memory address of the pointer p, which is stored as a local variable in the stack. Lastly, the "p" will printout the memory address where the value is stored in the heap. I am looking forward for your inquiries. Angelos Skiadopoulos

15th Jun 2018, 12:31 PM
Angelos Skiadopoulos
Angelos Skiadopoulos - avatar
2 Answers
17th Jun 2018, 1:24 PM
Azam
Azam - avatar
- 1
yes it is fine.
17th Jun 2018, 1:24 PM
Azam
Azam - avatar