Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stack Overflow

Status
Not open for further replies.

jimthecanadian

IS-IT--Management
Jul 22, 2003
52
0
0
CA
I am writing an assignment for school and I have run into a glitch. My program generates a stack overflow as soon as it runs the constructor for the object i am creating. The program does use recursion but it never gets to the recursive call (and it is not very deeply recursed anyway). Can anyone give me an idea of where to look?

here are the variables and constructor. For any value larger that 20 or so i get the overflow error.

Code:
public class deterministicSelect {
	
	int[] randomArray;				//array to be selected
	int arraySize;					// size of above
	int spatializer = 200;			// ratio to increase array size by for random number generation
	int[] arrayOfMedians = new int[400];
	Random ranGen = new Random();	// Random number generator
	PriorityQueue<Integer> distinct = new PriorityQueue<Integer>(); // heap to track distinct elements in number generation

//-------------------------------------------------------------------------------------------
// Constructor --
//-------------------------------------------------------------------------------------------
	
	public deterministicSelect(int n){
		
		randomArray = new int[n];  // instantiate array to size n
		arrayOfMedians = new int[n/5+1];
		arraySize = n;				// easier than writing randomArray.length() all the time
		fillRandomArray();			// function to fill array with distinct random numbers
		
	}
 
Try not initializing arrayOfMedians with int[400]. Only initialize it in the constructor.
 
That is what i did initially. I am gettign a little closer to the answer. It turns out the overflow is directly related to the size of that array. I use it in a recursive call later. I suspected the problem was in the constructor because when i run the debug it craps out before gettign to the recursive call. but upon closer inspection of the error it seems it is happening in this function


Code:
	private int getMedian(int[] medArray, int start, int end){
		int selectSize = 5;
		int medCount = 0; 	
		for (int x = start; x < end ;x=x+selectSize ){
			arrayOfMedians[medCount++] = insertionSort(medArray,x, Math.min(x+selectSize-1,end));
		}
		
		if (arrayOfMedians.length <= selectSize)
			return insertionSort(arrayOfMedians,0, medCount-1);
		else
			getMedian(arrayOfMedians,0, medCount-1);				
		
	}

Now, am i gettng the error because it is recursing too deeply? Given that I get the overflow when the size of the initial array is 40 this function should not recurse more than 3 or 4 times.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top