StackOverflowException未处理

时间:2012-01-07 03:33:17

标签: c++

使用此代码时出现堆栈溢出错误,如果我使用head=0,则会询问新运算符。

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;
     } 

1 个答案:

答案 0 :(得分:4)

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

构建EventList时,它会开始构建另一个EventList。由于您不终止递归,因此最终会耗尽堆栈空间。

(即使你终止了递归,你也会泄漏你创建的所有这些EventList个对象,因为你没有在构造函数之外存储它们的指针。)