so im working on a project dealing with the heap, and i have 2 heaps in my driver. 1 i create and destroy in the main function and the other in a function. when i print out the values in the first one i am fine. but when I am printing out the last value in the second one I get the error that heap corruption detected after normal block(#133). what do i need to do to fix it here is my code: the .h file #include
using namespace std;
class Heap
{
int size;
int count;
int* num;
public:
//pre-conditions:none
//Post-Conditions:the heap has been inizialzed
//as well as the size, and count variables
Heap();
//pre-Conditions:none
//post-conditions:the heap, and other member variables
//have been initizialized
Heap(int limit);
//pre-conditions:the array isnt null
//Post-Conditions:the heap is destoryed and
//the memory is freed up
~Heap();
//Pre-Conditions:the array is not full
//Post-Conditions:if the array is full the array grows
//and adds the item in the correct spot
//if not the item is added in the correct spot in the arry, so that it is still a heap
void insert(int item);
//pre-conditions:There is at least something in the array
//post-Conditions:the item in the root of the tree has been removed and the tree has a new root
int removeFront();
//pre-conditions:the heap isnt empty
//post-conditions:the heap has been printed out
void print();
//pre-conditions:the tree isnt empty
//post-conditions:the tree has been printed using a tabbed view
void tabbedPrint();
//pre-conditions:none
//Post-Conditions the tree has been printed using a tabbed view
void tabbedPrint(int position, int spaces);
//private member functions
private:
//pre-conditions:we are trying to add an item to the array, but it is full
//post-Conditions:the array has doubled in size
void resize();
};
Heap::Heap()
{
const int LIMIT = 10;//used to size the array
num = new int[LIMIT];//declares the array and its size
size = LIMIT;//declares the size of the array
count = 0;//declares the number of items in the array
}
Heap::Heap(int limit)
{
num = new int[limit];//inizitalizes the array
size = limit; //sets the size of the array
count = 0;//declares how many items have been deleted
}
Heap::~Heap()
{
if(num != NULL)//if the array has been inizialized
{
delete [] num;//delete the array
num = NULL;//set the pointer to null
}
}
void Heap::insert(int item)
{
const int HALF = 2;//used to find the parents in the array
if(count == size)//if the array is full
{
resize();//resizes the array
}
if(count == 0)
{
count++;
num[count] = item;//adds the item to the array at the first available location
}
else
{
count++;//increment the number of items in the array
num[count] = item;//adds the item to the array at the first available location
int position = count;//used to tranverse the array
while(num[position] > num[position / HALF])//if the child is greater than the parent
{
int tmp = num[position];//tmp storage
num[position] = num[position / HALF];//swap the child and the parent
num[position / HALF] = tmp;//swap the parent and the child
position = position / HALF;//half the position in the array
}
}
cout<<endl;
for(int i = 1; i <= count; i++)
{
cout<<num[i]<< " ";
}
}
int Heap::removeFront()
{
const int DOUBLE = 2;//used to calculate where we are in the array
int root = num[1];//copies the current root
num[1] = num[count];//copies the last entry to the root
num[count] = NULL; //sets the array item to null
count--;//decrement the number of items in the tree
if(count == 0)//if there was only one item in the tree
{
return root;//return the nummber taken out
}
else
{
int position = 1;//used to transverse the array
while((num[position] < num[position * DOUBLE]) || (num[position] < num[(position * DOUBLE) +1]))//if the position is less than one of its children
{
if(num[position * DOUBLE ] > num[position * DOUBLE + 1])//if the left child is higher
{
int tmp = num[position];//use for temp storage
num[position] = num[position * DOUBLE];//swap the first and the second
num[position * DOUBLE] = tmp;//swap the second and the first
position = position * DOUBLE;//calculates a new position in the array
}
else
{
int tmp = num[position];//used for temp storage
num[position] = num[position * DOUBLE +1 ];//swap the first and the second
num[position * DOUBLE +1] = tmp;//swap the second and the first
position = position * DOUBLE +1;//calculate a new position in the array
}
}
return root;//return the number taken out
}
}
void Heap::print()
{
if(count != 0)//if the tree isnt empty
{
for(int i = 1;i <= count; i++)//process each item in the array
{
cout<<num[i]<< " ";//output the list to the screen
}
cout<<endl;//put a newline to the screen
}
}
void Heap::tabbedPrint()
{
int position = 1, spaces = 1; //tells us where we are int the array
tabbedPrint(position, spaces);//passes the variable to the function
}
void Heap::tabbedPrint(int position, int spaces)
{
const int WIDTH = 4, DOUBLE = 2; //declare some consts
cout<<setw(WIDTH * spaces)<<"";//sets the inpendt of the leaf
if(position > count)//if we are outside the number of items on the tree
{
cout<<"[Empty]"<<endl;//output to the screen
}
else if(position * DOUBLE > count ) //if we are at a leaf
{
cout<<num[position];//output to the screen
cout<<" [leaf]" <<endl;//output to the scree
}
else
{
cout<<num[position]<<endl;//output to the screen
tabbedPrint(position * DOUBLE , spaces +1);//acess the left child
tabbedPrint(position * DOUBLE +1, spaces+1);//acess the right child
}
}
void Heap::resize()
{
const int DOUBLE = 2; //used to double the size of the array
int newSize = size * DOUBLE;//double the member variable
int* tmp = new int[newSize];//delcares a tmp array
for(int i = 1; i < size; i++)//process each item in the old array
{
tmp[i] = num[i];//copies the items from the array
}
size = newSize;//sets the data member
num = tmp;//sets the original array to the new one
delete [] tmp;//deletes the memory for the array
}
and the driver:
#include<iostream>
#include"heap.h"//includes the heap class
using namespace std;
//pre-conditions:The heap has been inizialized
//post-Condtions:the menu has been printed
void printMenu();
//pre-conditions:the array is filled with a bunch of random numbers
//post-conditions: the array is now a heap
void heapSort( int nums[], int size);
int main()
{
const int SIZE = 10;
char input; //used for the switch and to inizialize the heap
int newNum;//used to add a num to the heap
Heap numbers;//declares the heap
do//repeat
{
printMenu();//print out the menu
cin>>input;//user inputs their choice
input = toupper(input);//capitalize their choice
switch(input)//switch on what they input
{
case'E':numbers.~Heap();//calls the deconstructor
break;//exit the switch
case'I':cout<<"What number did you want to add to the heap"<<endl;//output to the screen
cin>>newNum;//user inputs the new number
numbers.insert(newNum);//add the number to the heap
break;
case'P':numbers.print();//prints the contents of the array
break;//exit
case'R':numbers.removeFront();//removes an item from the front of the array
break;//exit
case'T':numbers.tabbedPrint();//calls the function to print it out like a tree
break;//exit
default: cout<<input<<" is invalid"<<endl;//output to the screen
break;//exit the loop
}
}while(input != 'E');//repeat until they hit exit
int num[SIZE] = { 45,42,23,27,35,22,4,19,5,20};//declare an array
/*for(int i = 1; i <= SIZE; i++)
{
num[i] = rand() % 101 + 1;
}*/
heapSort(num, SIZE);
system("Pause");//pauses for user input
return 0;//exits the function
}
void printMenu()
{
cout<<endl;//output to the screen
cout<<"The Heap Menu"<<endl;//output to the scree
cout<<"The Following options are availiable"<<endl;//ouput to the screen
cout<<"E-Exit the program"<<endl;//output to the screen
cout<<"I-Insert a number onto the heap"<<endl;//output to the screen
cout<<"P-Print the items on the heap"<<endl;//output to the screen
cout<<"R-Remove the item from the front of the heap"<<endl;//output to the screen
cout<<"T-Print the heap in as a tabbed tree"<<endl;//output to the screen
}
void heapSort(int numbers[], int size)
{
Heap random(size);//declare the heap variable
for(int i = 0; i < size; i++)//process each item in the array
{
random.insert(numbers[i]);//add it to the heap
}
random.print();//prints the array
}
any help would be appreciated thanks