2012-01-07 85 views
-3

使用此代碼,我得到一個堆棧溢出錯誤,如果我使用head=0它要求新的操作符。StackOverflowException未處理

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

你學到了什麼時你在調試器中遍歷你的代碼? – 2012-01-07 03:35:23

+1

爲什麼要更換新的操作員?爲什麼在包含預編譯頭文件之前有代碼?爲什麼你不使用C++標準庫容器中的一個作爲你的列表? – 2012-01-07 03:48:24

回答

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

EventList構造,它開始構建另一EventList。由於您不會終止遞歸,因此最終會耗盡堆棧空間。

(即使你終止了遞歸,你會泄露所有youv'e創建這些EventList的對象,因爲你不指針把它們存放到構造之外。)

+0

如果我使用head = 0;它給我的錯誤對象引用沒有設置使用new來創建對象引用。 – snasr 2012-01-07 11:16:48

+0

這將是你最好的[一本好的入門C++書](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)。 – 2012-01-07 18:34:16

+0

我知道它也有一些錯誤和錯誤,但我想我的模型的最佳解決方案...在此先感謝 – snasr 2012-01-08 07:36:26