Stack overflow

I'm trying to time the quicksort and selection sort methods on my machine and my arrays are too big to fit on the stack. I've been trying to research ways to solve this but am struggling to implement what I've read. Here's some code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#ifndef PROGRAM1_H_
#define PROGRAM1_H_
#include "stdafx.h"
#include "timer.h"
#include "quicksort.h"
#include <stdlib.h>
#include <vector>
/*
 * Description: This class times the selection sort method for different
 * array lengths.
 */

int main(){
		//creates three arrays with lengths 10000,100000,
		//and 1000000 respectively and initalizes them.
		int array1_size = 10000;
		int array2_size = 100000;
		int array3_size = 1000000;

		int array4_size = 10000;
		int array5_size = 100000;
		int array6_size = 1000000;

		int *array1 = new int [array1_size];
		int *array2 = new int [array2_size];
		int *array3 = new int [array3_size];

		int *array4 = new int [array4_size];
		int *array5 = new int [array5_size];
		int *array6 = new int [array6_size];

		//std::vector<std::vector<int> > array1[array1_size]; (this is me fooling around)
		//std::vector<std::vector<int> > array2;
		//std::vector<std::vector<int> > array3;
		//std::vector<std::vector<int> > array4;
		//std::vector<std::vector<int> > array5;
		//std::vector<std::vector<int> > array6;

        Timer t;
		for (int x=0; x<array1_size; x++){ //loop 10000 times.
			array1[x] = ((rand()*rand() % 1000000)+1); //sets slot x of array1 to a random number.
			t.Start(); //starts the timer.
			quickSort(array1,0,array1_size-1); //calls quicksort supplying the array and range to sort.
			t.Stop(); //stops the timer
		}

		cout<< "(quicksort) size: "<< array1_size <<" time: "<< t.Seconds()<<" seconds"<<endl ; //prints result
		t.Reset(); //resets the timer back to 0
		delete [] array1; //clean up

		for (int x=0; x<array2_size; x++){ //loops 100000 times
			array2[x] = ((rand()*rand() % 1000000)+1); //assigns random int to slot x of array2
			t.Start();
			quickSort(array2,0,array2_size-1); //calls quicksort
			t.Stop();
		}
		cout << "(quicksort) size: " << array2_size <<" time: " << t.Seconds() << " seconds"<<endl; //prints array2 results
		t.Reset();

		delete [] array2; //clean up

		for (int x=0; x<array3_size; x++){ //loops 1000000 times
			array3[x] = ((rand()*rand() % 1000000)+1); //assigns a random number to each slot in array3
			t.Start();
			quickSort(array3,0,array3_size-1); //calls the quicksort method
			t.Stop();
		}
		cout << "(quicksort) size: " << array3_size <<" time: " << t.Seconds()<< " seconds"<<endl; //prints the result
		t.Reset();
		
		delete [] array3; //clean up

		for (int x=0; x<array4_size; x++){ //loops 10000 times
			array4[x] = ((rand()*rand() % 1000000)+1); //assigns a random number to each slot in array4
			t.Start();
			selectionSort(array4,array4_size); //calls the selection sort method
			t.Stop();
		}
		cout << "(selectionsort) size: " << array4_size <<" time: " << t.Seconds()<< " seconds"<<endl; //prints the result
		t.Reset();

		delete [] array4; //clean up

		for (int x=0; x<array5_size; x++){ //loops 100000 times
			array5[x] = ((rand()*rand() % 1000000)+1); //assigns a random number to each slot in array5
			t.Start();
			selectionSort(array5,array5_size); //calls the selection sort method
			t.Stop();
		}
		cout << "(selectionsort) size: " << array5_size <<" time: " << t.Seconds()<< " seconds"<<endl; //prints the result
		t.Reset();

		delete [] array5; //clean up

		for (int x=0; x<array6_size; x++){ //loops 1000000 times
			array6[x] = ((rand()*rand() % 1000000)+1); //assigns a random number to each slot in array6
			t.Start();
			selectionSort(array6,array6_size); //calls the selection sort method
			t.Stop();
		}
		cout << "(selectionsort) size: " << array6_size <<" time: " << t.Seconds()<< " seconds"<<endl; //prints the result
		t.Reset();

		delete [] array6; //clean up
	}
	


All help is appreciated!
Last edited on
These arrays are on the heap, not the stack.

Can't you just do one at a time? That way only one array needs to be in memory at a time.
Trying 1 at a time I got the same error. Here's what it looks like now for the first 2 loops:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
int main(){
		//creates three array lengths 10000,100000,
		//and 1000000 respectively.
		int array1_size = 10000;
		int array2_size = 100000;
		int array3_size = 1000000;

		int array4_size = 10000;
		int array5_size = 100000;
		int array6_size = 1000000;

		int *array1 = new int [array1_size]; // creates and initializes array1

        Timer t;
		for (int x=0; x<array1_size; x++){ //loop 10000 times.
			array1[x] = ((rand()*rand() % 1000000)+1); //sets slot x of array1 to a random number.
			t.Start(); //starts the timer.
			quickSort(array1,0,array1_size-1); //calls quicksort supplying the array and range to sort.
			t.Stop(); //stops the timer
		}

		cout<< "(quicksort) size: "<< array1_size <<" time: "<< t.Seconds()<<" seconds"<<endl ; //prints result
		t.Reset(); //resets the timer back to 0
		delete [] array1; //clean up

		int *array2 = new int [array2_size];
		for (int x=0; x<array2_size; x++){ //loops 100000 times
			array2[x] = ((rand()*rand() % 1000000)+1); //assigns random int to slot x of array2
			t.Start();
			quickSort(array2,0,array2_size-1); //calls quicksort
			t.Stop();
		}
		cout << "(quicksort) size: " << array2_size <<" time: " << t.Seconds() << " seconds"<<endl; //prints array2 results
		t.Reset();

		delete [] array2; //clean up 


Are the array_size ints causing the issue now?
Last edited on
If by "causing a problem" you mean "containing very large numbers", then I guess you could say that. But really, are the smaller numbers so fast on your machine that the calculation is not accurate enough? (If so, you should probably give NASA their computer back...)
Last edited on
Topic archived. No new replies allowed.