C++ Object Creation and Memory Allocation

前端 未结 5 1310
一整个雨季
一整个雨季 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 10:49

    Firstly, you should write:

    Movie firstMove("Titanic", 126, 13.2); // Created on the stack
    Movie *pFirstMovie = new Movie("Titanic", 126, 13.2); // Created on the heap
    

    Otherwise you create an object and then copy it to firstMovie and that means you need to have a copy constructor.

    The main difference is that the first example creates a object on the stack and the second creates it on the heap. When you exit the function where the object was created the objects on the stack will be destroyed and any reference to this object will become invalid.

    So, if you only need to use the object within the function then the object is best created on the stack, this way you do not need to worry about memory leaks.

    However, if you want to use the object in other places it is best to create it on the heap, using the new operator. You can freely pass references around but need to remember to delete it later.

    One more advantage of creating object on the heap is that when you require a lot of data, or have recursion, you will run of stack space much faster than that you run out of heap space.

    0 讨论(0)
  • 2021-01-28 10:58

    I think you understand the difference between the two fine. Instead, the question is "which should I prefer?" Personally, if I can get away with it, I prefer to put things on the stack. Nowadays your stack is large enough that you won't get a stack overflow unless you have some other bug, and putting things on the stack means (1) there's no need to delete it by hand, (2) it's more likely to be in the memory cache than stuff on the heap, and (3) allocating the memory is as fast as you can get.

    However, there are times that you can't get away with it. In those cases, go ahead and put things on the heap.

    The confusion comes from Gosling's strange insistence on using new to mean "make me an object." Java documentation always brags about how you don't need to delete things that you new-ed up (because of the garbage collector) but never seems to mention that in C++ you don't have to new things up nearly as often as in Java.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-28 11:05
    Movie firstMovie = Movie("Titanic", 126, 13.2);
    

    You are creating an object by calling constructor, which inturn gets assigned to firstMovie. Better way to do this will be

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

    In second case

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

    You are dynamically creating an object on heap you need to delete this object using delete once its no more required.

    delete firstMovie;

    0 讨论(0)
  • 2021-01-28 11:06

    You can simply instantiate the object like this:

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

    You have to use the new keyword when you are initializing raw pointers.

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

    The difference is that the pointer object uses -> when accessing member functions while the other object uses the . notation. Also the pointer object is allocated in the heap while the other one is allocated in the stack.

    0 讨论(0)
提交回复
热议问题