7

What's the difference between doing,

EventList temp;

EventList* temp = new EventList();

Where now temp you access it's vars by doing using . and the other is ->

Besides that difference, what else? Pointer allocates on the heap while just EventList is on the stack. So is it primarily a scope thing?

2
  • 2
    If you don't already have one, you need to get a good introductory C++ book. Commented Feb 18, 2011 at 21:20
  • 4
    What is the difference between being something and pointing at something? Being a king or point at a king? Commented Feb 18, 2011 at 21:22

3 Answers 3

7

There is short summary

Object on the stack EventList temp;

  • access is little bit faster, there is no derefferencing
  • object are automatically deleted at the end of method which creates them so we don't have to care about their deletion
  • stack size is limited ( much more than heap )
  • these objects cannot be returned from the method without copying

Object on the heap EventList* temp = new EventList();

  • heap is "unlimited" ( comparing to stack )
  • these objects are easilly shared accross whole application because they aren't automatically deteled
  • we have to delete them manually, if we loose the pointer then there are lost bytes in memory ( leaks )
Sign up to request clarification or add additional context in comments.

Comments

4

Eventlist temp is allocated and deallocated automatically within the scope in which it is called. That is, when you run the following code:

{
    EventList temp;
}

The default constructor for EventList is called at the point of its declaration, and the destructor is called at the end of the block.

EventList *temp = new EventList(); is allocated on the heap. You can read more about it here.

3 Comments

No, EventList temp is not always allocated on the stack. Consider the case where it's a member variable of an object which has been allocated on the heap.
Hopefully that's better. Let me know if I'm missing something here.
The point about (local auto) variables being on the stack was worthwhile, though. The top of the stack is probably always in the cache.
1

Well, from that example, there are only things that you have noted that are differences, but when using virtual functions and inheritance you'll see differences.

For example, here is the same code with pointers and without:

WITH POINTERS:

#include <iostream>
using namespace std;
class Base
     {
       public:
          virtual void Create()
          {
               cout <<"Base class create function ";
          }
     };
     class Derived : public Base
     {
       public:
          void Create()
          {
              cout<<"Derived class create function ";
          }
     };
     int main()
     {
         Base  *x, *y;  
         x = new Base();
         y = new Derived();         
         x->Create();
         y->Create();
         return 0;
     }

OUTPUT: Base class create function Derived class create function

WITHOUT POINTERS:

#include <iostream>
using namespace std;
class Base
     {
       public:
          virtual void Create()
          {
               cout <<"Base class create function ";
          }
     };
     class Derived : public Base
     {
       public:
          void Create()
          {
              cout<<"Derived class create function ";
          }
     };
     int main()
     {
         Base  x, y;  
         x.Create();
         y.Create();
         return 0;
     }

OUTPUT: Base class create function Base class create function

So there are problems with objects and virtual functions. It is executed base class function when derived one should be. That is one of differences.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.