Quantcast
Channel: Visual C forum
Viewing all articles
Browse latest Browse all 15302

Strange runtime error in visual studio 2013 update 2, code runs fine in VS2010(not tried in other versions)

$
0
0
Assalam o Alaikum:
I am a student from pakistan.
I am having some strange runtime error which seems to be wrong. Actually i was coding a simple min heap program as an assignment(which has been submitted last week). And i was just stuck on that runtime error on deletion of array.
# include <iostream>

using namespace std;

template <class type>
class heap{
	int MAX_SIZE;   //maximum elements it can store 
	type* aray;     //array to store elements of heap 
	int currIndex;  //current empty index of the heap where next
	// insertion should be made 
	int cal_size(int n){
		int tmp = 2;
		while (tmp < n){
			tmp *= 2;
		}
		return tmp;
	}
	int min_child(int i){
		if (i >= currIndex)
			throw - 1;
		if (aray[left(i)] > aray[right(i)]){
			return right(i);
		}
		else
			return left(i);
	}
public:
	explicit heap(int s = 100){
		s = cal_size(s);
		MAX_SIZE = s;
		aray = new type[s];
		currIndex = 1;
	}
	heap(){
		aray = new type[2];
		MAX_SIZE = 1;
		currIndex = 1;
	}
	heap(const heap & obj){
		MAX_SIZE = obj.MAX_SIZE;
		currIndex = obj.currIndex;
		aray = new type[MAX_SIZE];
		for (int i = 1; i < currIndex; i++)
			aray[i] = obj.aray[i];
	}
	~heap(){
		if (aray != NULL)
			delete[] aray;
	}
	int left(int i){
		if (i < MAX_SIZE)
			return i * 2;
		throw - 1;
	}
	int right(int i){
		if (i < MAX_SIZE*2)
			return (i * 2)+1;
		throw - 1;
	}
	int parent(int i){
		return i / 2;
	}
	void heapifyUp(int i){
		int p = parent(i);
		while (aray[i] < aray[p]){
			swap(aray[i], aray[p]);
			i = p;
			p = parent(i);
			if (p < 1)
				break;
		}
	}
	void insert(type key){
		aray[currIndex] = key;
		heapifyUp(currIndex);
		currIndex++;
		if (currIndex > MAX_SIZE){
			MAX_SIZE = MAX_SIZE * 2;
			type * tmp = aray;
			aray = new type[MAX_SIZE];
			for (int i = 1; i < currIndex; i++)
				aray[i] = tmp[i];
			//delete[] tmp;  //I am getting strange runtime error on this line!! if you read this line please inform me what is that error!
		}
	}
	void HeapifyDown(int i){
		int ch =left(i);
		while (ch < currIndex){
			if (aray[i] > aray[ch] || aray[i] > aray[ch + 1]){
				int r = min_child(i);
				swap(aray[r], aray[i]);
				i = r;
				ch = left(i);
			}
			else
				break;
		}
	}
	type Remove(){
		if (currIndex < 2)
			throw - 1;
		type tmp = aray[1];
		aray[1] = aray[--currIndex];
		HeapifyDown(1);
		//currIndex--;
		return tmp;
	}
	void sort(){
		for (int i = currIndex / 2 + 1; i > 0; i++)
			HeapifyDown(i);
	}
	void print()
	{
		for (int i = 1; i < currIndex; i++)cout << aray[i]<<',';
		cout << endl;
	}
};


int main(){
	do{
		cout << "Please enter the size of heap you want. ";
		int size,choice;
		cin >> size;
		heap<int> hp(size);
		do{
			cout << "Please enter the number of desired operation:\n1) insert\n2) delete min\n3) finish\n";
			cin >> choice;
			if (choice == 1){
				int n;
				cout << "Enter the value to insert. ";
				cin >> n;
				hp.insert(n);
				hp.print();
			}
			else if (choice == 2){
				try{
					cout << "removed value: " << hp.Remove() << endl;
				}
				catch (int i)
				{
					cout << "\n\t\tERROR!\n Heap is probably empty...\n\n";
				}
				hp.print();
			}
			else if (choice == 3)
				break;
			else
				cout << "Wrong choice!!\n";

		} while (true);
		cout << "Do you want to makenew heap??\ny//n ";
		char c;
		cin >> c;
		if (c == 'n' || c == 'N')
			break;
	} while (true);
	return 0;
}
Here in insert function you can see a delee[] line commented.
Try un-commenting and error will come.
May be i have some problem in code thats why i am discussing it here.
Need some fast answers because my papers are approaching and concepts should be clear before that :)  

Viewing all articles
Browse latest Browse all 15302

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>