-3

I get a stack overflow error using this code, and if I use head=0 it asks for the new operator.

void *__CRTDECL operator new(size_t size) _THROW1(_STD bad_alloc)
    {       // try to allocate size bytes
    void *p;
    while ((p = malloc(size)) == 0)
            if (_callnewh(size) == 0)
            {       // report no memory
            static const std::bad_alloc nomem;
            _RAISE(nomem);
            }
    return (p);
    }



  #include "StdAfx.h"
  #include "event.h"


   EventList::EventList()
       {
       size = 0;
       EventList *head = new EventList();
       tail = NULL;


    }


   EventList::~EventList()
    {
    //Do nothing
    }

    unsigned int EventList::Size()
        {
        return size;
        }

     void EventList :: add(Event* ev )
     /*the event will be added to the correct location based on its time
       So, always the event at the top (or at location 0) is the most
       imminent event that must be handled firstly*/

         if(size == 0)
             {
             head = tail = ev;
             ev->next = ev->previous = 0;
             }
         else
             {
             if(ev->eventTime < head->eventTime)
             {
             ///////////////  
             ev -> next = head; 
             head -> previous = ev;
             ev -> previous = 0 ; 
             head = ev; 
             ///////////////
             }
         else
             {
             //////////// 
             Event* tracer = head; 
             while ( tracer -> next != 0 ) 
                 {
                 if ( ev -> eventTime >= tracer -> eventTime )
                     {
                     tracer = tracer -> next; 
                     } 
                 else
                     {
                     ev -> next = tracer; 
                     ev -> previous = tracer -> previous; 
                     tracer -> previous -> next = ev; 
                     tracer -> previous = ev ;                          
                     } 
                 }
                 //////////////  
                 if ( tracer -> next == 0 ) 
                     {
                     ev -> next = 0 ;
                     ev -> previous = tracer ; 
                     tracer -> next = ev;
                     tail = ev; 
                     }
                 ////////////
             } 
        } 
        size++;
   } 
   //Remove the most imminent event from the event list to be handled
   //by the simulator
   //Since we have an ordered linked list this event is found at the head
   //of the event list
   //The retreived event will be found in ev
   bool EventList :: remove(Event* ev)
     {
     /*public  ev =new EventList();*/
     if(size == 0)
         {
         ev = 0;
         return false;
         }
     else
         {
         ev =head;      
         //head=ev;
         if ( size != 1 ) 
             {
             //head -> next -> previous = 0; 
             head = head -> next ;
             ev -> next = ev -> previous = 0; 
             }
         else
             {
             head = tail = 0; 
             } 
         return true;
         }
     delete head;
     head=NULL;
     } 
Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
snasr
  • 19
  • 2

1 Answers1

4
EventList::EventList()
{
    // ...
    EventList *head = new EventList();
    // ...
}

When an EventList is constructed, it starts constructing another EventList. Since you do not terminate the recursion, you will eventually run out of stack space.

(Even if you terminated the recursion, you would leak all of these EventList objects that youv'e created, since you don't store the pointers to them outside of the constructor.)

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • if i use head=0; it gives me the error object reference is not set use new to create object reference. – snasr Jan 07 '12 at 11:16
  • It would behoove you to get [a good introductory C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – James McNellis Jan 07 '12 at 18:34
  • I know it has some errors and mistakes also but i want the best solution of my model...Thanks in advance – snasr Jan 08 '12 at 07:36
  • in your opinion what is the best code so that i can try..thnkas – snasr Jan 08 '12 at 15:00
  • Sure, lots of people here could write this program. But that's not what we do. You need to get a good introductory C++ book and learn the language. See the link I posted in a comment above for a list of good introductory books from which to choose. – James McNellis Jan 21 '12 at 19:19