What's the difference between this {MyClass obj; MyClass *ptr = &obj;} and this {Myclass *ptr=new Myclass}?.... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What's the difference between this {MyClass obj; MyClass *ptr = &obj;} and this {Myclass *ptr=new Myclass}?....

C++ question

2nd Sep 2022, 10:30 AM
Emediong Ekoh
Emediong Ekoh - avatar
1 Answer
+ 2
*ptr = &obj Here the pointer holds the address of an object allocated on the stack. You can also think of this as some sort of alias for your variable *ptr = new Myclass Here the pointer holds the address of an object allocated on the heap
2nd Sep 2022, 1:36 PM
giugiu
giugiu - avatar