"Program.exe has stopped working"

Hi there! I'm relatively new to C++ and I definitely don't know as much as I should. Case in point: I'm programming a pretty simple, 2D matrix addition program that adds two arrays of random numbers together and stores the result in a third matrix. The program also times the overall operation, because eventually, I want to use this program as a comparison point for a CUDA program of the same purpose. To this end, I want the matrices involved to be rather big.

The code compiles and runs as is, but if I increase the number of rows or columns by any significant amount, the program compiles and then crashes a few seconds later ("2d_Matrix_Adder.exe has stopped working"). I've tried to figure out what is going wrong, but everything seems syntactically correct, making me think it's some conceptual problem (memory allocation? etc.) that's currently over my head.

Here's the 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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>

using namespace std;

// Global variables.
const int NUM_ROWS = 50;				// The number of rows in the matrices.
const int NUM_COLUMNS = 1000;				// The number of columns in the matrices.

// Populates the given matrix with random values in each slot.
void populateMatrix(int matrix[NUM_ROWS][NUM_COLUMNS]) {
	for (int i = 0; i < NUM_ROWS; ++i) {
		for (int j = 0; j < NUM_COLUMNS; ++j) {
			matrix[i][j] = rand() % 1000;
		}
	}
}

// Prints the first 5 rows of a given matrix to the screen.
void printMatrix(string message, int matrix[NUM_ROWS][NUM_COLUMNS]) {
	cout << message << endl;
	for (int i = 0; i < 5; ++i) {
		for (int j = 0; j < 5; ++j) {
			cout <<  matrix[i][j] << " ";
		}
		cout << endl;
	}
	cout << endl;
}

// Adds two matrices together and stores the result in a third one.
void addMatrices(int one[NUM_ROWS][NUM_COLUMNS], int two[NUM_ROWS][NUM_COLUMNS], int result[NUM_ROWS][NUM_COLUMNS]) {
	for (int i = 0; i < NUM_ROWS; ++i) {
		for (int j = 0; j < NUM_COLUMNS; ++j) {
			result[i][j] = one[i][j] + two[i][j];
		}
	}
}
int main() {
	// Calculates the start time of the program.
	long startTime = clock();
	// Seeds the random function with a time dependent value, for pseudo-random numbers.
	srand(time(NULL));

	int m_a[NUM_ROWS][NUM_COLUMNS] = { {0} };
	int m_b[NUM_ROWS][NUM_COLUMNS] = { {0} };
	int m_c[NUM_ROWS][NUM_COLUMNS] = { {0} };

	populateMatrix(m_a);
	populateMatrix(m_b);

	printMatrix("Matrix a before addition:", m_a);
	printMatrix("Matrix b before addition:", m_b);

	addMatrices(m_a, m_b, m_c);

	printMatrix("After addition:", m_c);

	// Calculates the finish time of the program and reports the overall time taken.
	long finishTime = clock();
	cout << "C++ took " << finishTime - startTime << " millis." << endl;

	// Exit program.
	return 0;
}


Any help would be much appreciated. Also, if you see anything unrelated that you think needs fixing or you think would benefit me to know, feel free to tell me! Thanks much!

EDIT: Following the below advice, I looked at the memory usage and had a friend help with debugging and found out that there was an issue with stack overflow. I managed to resolve it by making use of pointers.
Last edited on
50,000 ints is a lot of memory in one spot. Increase that and you will have memory problems.

A way to fix it is to not use simple arrays. Use something (like a deque <vector <int> >) that the computer can use to play with memory more easily.

Have you considered using a C++ matrix library? A good number exist, including some that give you nice sparse matrices.

Hope this helps.
Thanks very much for the reply! You were right - it ended up being a memory overflow on the stack. I played around with using vectors a bit, but ended up just making use of pointers to the matrices for functions instead, after a bit of help from my friend. That seemed to circumnavigate the problem - I can now enter matrices with dimensions like 10,000 by 10,000.

I appreciate the help!
am i miscalculating or something? according to my calculation, the size of 50,000 ints are 200KB, and for me, it's not big size for a program.

1
2
3
4
5
6
7
8
#include <iostream>
#include <conio.h>
int main () {
	std::cout << "size of 50,000 ints are: " << (sizeof (int) * 50) << "KB" << std::endl;
	std::cout << "press any key to continue. . .";
	getch();
	return 0;
}


am i missed something?
To the best of my (limited) knowledge, it wasn't necessarily a memory problem for the overall program - I ran it with performance manager on and barely noticed a change in my system's memory usage for 50,000 ints. The friend who helped me out indicated the problem was a stack overflow - not from recursion, but just from using more memory than the stack was allocated. Not sure if I transcribed the concept as he described it, haha, but that's the best I can do.
Oh! Doh! Of course, all those arrays are local to main()...

Glad you got the problem resolved.
Topic archived. No new replies allowed.