of course.
the first one is an object created and allocated from the stack. it will be deleted automatically when its scope is over.
that has being said:
- you can't return it as pointer or reference - because then you'll return a memory address which does not point to a valid object
- if you return it, it will return as copy or move
- you don't have to be afraid of memory leaks, becuase it's de-allocated automatically
- allocating it is much faster then the alternative heap allocation
the second one is allocated from the heap. a heap is a huge memory block given to you by the OS. that has being said:
- an object allocated with new will live on untill you call
delete
on it
- it's scope is universal to the program
- you must pass it as a pointer or reference (well, you should, anyway)
- you must
delete
it on some point
- heap allocation is slower
- the dangers of memory leak is greater
there are many many other neuanses to these, including polimorphism, multi-threaded enviroments and much more.
learn about memory managment, know it as the palm of you hand , but opt unique_ptr
and shared_ptr
when time comes by.