LINUX.ORG.RU

Сообщения GooseUSS

 

Ошибка исполнения, corruption of the heap

Помогите пожалуйста, не могу понять в чём ошибка, программа компилируется, исполняется, но когда доходит до return 0; в main(), то происходит ошибка исполнения, что то вроде «corruption of the heap». Единственное место где выделяю и уничтожаю память это класс PriorityQueue. Но там вроде всё правильно, нигде ничего не порчу. Помогите пожалуйста. Заранее спасибо.

---------------------------------------------------------------

main.cpp

---------------------------------------------------------------

#include<iostream.>

#include"PriorityQueue.h"

int main()

{

      Maze* labirynth = new Maze(100, 100);

      labirynth->CreateMaze();

      delete labirynth;

      PriorityQueue<int, 10> q;

      for(int i=0; i<10; i++)

         q.InsertWithKey(i, 10 - i);

      for(int i=0; i<10; i++)

         std::cout<<q.Get()<<std::endl;

      std::cout<<«fuck you»<<std::endl;

      return 0;

}

---------------------------------------------------------------

PriorityQueue.h

---------------------------------------------------------------

#include"Exeption.h"

#include<iostream>

template <class Type, int Size>

class PriorityQueue{

   struct Element{

      int key;

      Type data;

   };

   Element *buffer;

   int length;

public:

   PriorityQueue(){

      try{

         buffer = new Element[Size];

      }catch (Exeption){std::cout<<«Error : Not enough memory, cannot create a queue, the program will now terminate»<<std::endl; exit(0);}

      length = 1;

   };

   ~PriorityQueue(){

      delete (buffer);

   };

   void InsertWithKey(Type element, int key){

      try{

         buffer[length].data = element;

         buffer[length].key = key;

         int father;

         int newElement = length;

         Element temp;

         length++;

         while(true){

            if(newElement != 1) father = newElement / 2;

            else break;

            if(buffer[father].key > buffer[newElement].key){

               temp = buffer[father];

               buffer[father] = buffer[newElement];

               buffer[newElement] = temp;

               newElement = father;

            }

            else break;

         }

      } catch(Exeption){std::cout<<«Error : Stack overflow, the program will now terminate»<<std::endl; exit(0);}

   };

   Type Get(){

      Element returnValue = buffer[1];

      buffer[1] = buffer[length-1];

      length--;    

      int left;

      int right;

      int element = 1;

      int min;

      Element temp;

          while(true){

         left = 2*element;

         right = 2*element + 1;

         if((left >= length) && (right >= length)) break;//the element is already on his place, because his at the bottom of the tree

                if(right >= length){//there is left brunch, but no right brunch

            if(buffer[left].key < buffer[element].key){

               temp = buffer[left];

               buffer[left] = buffer[element];

               buffer[element] = temp;

               element = left;

               continue;

            }

            else break;

         }

         //both brunches exist

         if(buffer[left].key < buffer[right].key) min = left;

         else min = right;

         if(buffer[element].key > buffer[min].key){

            temp = buffer[min];

            buffer[min] = buffer[element];

            buffer[element] = temp;

            element = min;

         }

         else break;

      }

      return returnValue.data;

   };

   bool IsEmpty(){

      if(length == 1) return true;

      else return false;

   };

};

--------------------------------------------------------------------

GooseUSS
()

RSS подписка на новые темы