6

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?

moinudin
  • 134,091
  • 45
  • 190
  • 216
RoR
  • 15,934
  • 22
  • 71
  • 92
  • 2
    If you don't already have one, you need to get [a good introductory C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – James McNellis Feb 18 '11 at 21:20
  • 4
    What is the difference between being something and pointing at something? Being a king or point at a king? – Bo Persson Feb 18 '11 at 21:22

3 Answers3

6

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 )
Gaim
  • 6,734
  • 4
  • 38
  • 58
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.

LandonSchropp
  • 10,084
  • 22
  • 86
  • 149
  • 3
    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. – Oliver Charlesworth Feb 18 '11 at 21:27
  • Hopefully that's better. Let me know if I'm missing something here. – LandonSchropp Feb 18 '11 at 21:41
  • The point about (local auto) variables being on the stack was worthwhile, though. The top of the stack is probably always in the cache. –  Feb 18 '11 at 21:49
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.

Vajda
  • 1,795
  • 6
  • 39
  • 49