Error during a simple swap method?

When I call a simple swap method to swap two values in an array, my program crashes with an error. When the program reaches line 5 of this:

1
2
3
4
5
6
7
8
void HeapSort::swap(long theArray[], int first, int second) 
{
	int temp;

	temp = theArray[first];
	theArray[first] = theArray[second];
	theArray[second] = temp;
} //end swap 


It stops the execution and leaves this error message:

Unhandled exception at 0x00c63fb9 in Sorting Algorithms using arrays of data.exe: 0xC0000005: Access violation reading location 0x00707fe4.

It confuses me because temp, first, and last all seem to hold appropriate values. I tried changing all the parameters and temp to long instead of int but that did nothing (and using integers instead of longs worked just fine for my other sorting algorithms anyway... In my IDE they are the same size). Here are the sorting methods that call it:

1
2
3
4
5
6
7
8
_int64 HeapSort::sort() 
{
	numops = 0; //For counting operations later on
	heapsort(theData, maxSize); 
             //long *theData and int maxSize are protected members in the base
             //class that HeapSort is derived from.
	return numops;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void HeapSort::heapsort(long theArray[], int n) 
{
		//call buildHeap to turn the unordered array into a heap
	buildHeap(theArray, n);
	
	static int sortedPortion = 0;

	int last = n-1;
	while (sortedPortion < n)
	{
		    //remove the largest value (the root) and copy it into 
                      //the last (vacant) position in the array
		swap(theArray, theArray[0], theArray[last]);
		++sortedPortion;
		heapsort(theArray, n-1); //sort the unsorted portion, which is now one less.
	}

}


1
2
3
4
5
6
7
8
9
10
11
12
void HeapSort::buildHeap(long theArray[], int count) 
{
	int end = 1;
	
	while (end < count)
	{
		    //move up the node at index 'end' to the proper place 
                      //such that all nodes above the end index are in heap order
		trickleUp(theArray, 0, end);
		end = end + 1; //after trickling up the last node all nodes are in heap order
	}
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void HeapSort::trickleUp(long theArray[], int start, int end)
{
	//start represents the limit of how far up the heap to move.
	//end is the node to move up
	int child = end;
	while (child > start)
	{
		int parent = (child - 1) / 2;
		if (theArray[parent] < theArray[child]) //If it is out of max-heap order
		{
			swap(theArray, theArray[parent], theArray[child]);
			child = parent;
		}
	}
}


Thank you in advance for any suggestions and thought!
Last edited on
can you show me the initialization of 'theArray[]'? I'd actually like to see the class.

Also, what I believe that you are doing is overrunning the bounds of your array.

1
2
3
4
5
6
7
8
9
void HeapSort::swap(long theArray[], int first, int second) 
{
	int temp;

	temp = theArray[first];
	theArray[first] = theArray[second];
	theArray[second] = temp;
} //end swap 


on line one of the code in THIS post you have a long theArray[]. Ok thats fine. When your pass a double or an int pointer that is not an array[] you are saying, "ok, theArray is now pointing to that value at element zero". As you iterate or access that array you are saying theArray [n * 8], because arrays are sequential in memory, (a long is 8 bits on my machine), where n is the element you are accessing, well you only have one value that the array is pointing to and its at some random place in memory (depending on where the compiler allocates it). So when you iterate through it your array will actually point to garbage outside of your element 0. If your compiler puts that variable next to reserved memory (which looks to be the error you got) you will actually try to iterate to that reserved memory and the compiler says no (and this time, no means no).
Last edited on
That previous post might be all BS because I was looking at the wrong call but still kinda might apply in a weird way. I would still like to see your class definitions and your constructors though.
The compiler just wanted some bounds checking >.<

Thank you very much for your reply!
Topic archived. No new replies allowed.