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.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
usingnamespace std;
// Global variables.
constint NUM_ROWS = 50; // The number of rows in the matrices.
constint 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.
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.
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.