C++ Object Creation and Memory Allocation

前端 未结 5 1357
一整个雨季
一整个雨季 2021-01-28 10:14

Let\'s say I have a simple Movie object that just keeps track of a few data members of name, length and cost.

If in my driver file, I create a Movie object. Which is pre

5条回答
  •  死守一世寂寞
    2021-01-28 11:00

    Memory will be allocated for both:

    Movie firstMovie("Titanic", 126, 13.2);

    and

    Movie* firstMovie = new Movie("Titanic", 126, 13.2);

    the difference is first one will be destructed as soon as it comes out of scope where second one will remain same until you delete separately. The second method is called dynamic memory allocation and memory will be allocated in heap where for the first case memory will be allocated in stack.

提交回复
热议问题