Yet another magic square problem. I'm creating an odd magic square program in C++, and for some reason the program keeps giving a segmentation fault error and quitting. Here is the code:
#include <iostream>
using std::cin;
using std::cout;
#include <cstring>
using std::memset;
int *generateOddSquare(int n) {
if (n % 2 != 0 && n >= 3) {
int row = 0, col = n / 2, square = n * n;
int **matrix = new int *[n], *dest = new int[square];
memset(matrix, 0, sizeof(matrix[0][0]) * square);
for (int i = 1; i <= square; i++) {
matrix[row][col] = i;
if (i % n == 0)
row++;
else {
if (row == 0)
row = n - 1;
else
row--;
if (col == (n - 1))
col = 0;
else
col++;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
dest[(i * n) + j] = matrix[i][j];
}
}
return dest;
} else
return NULL;
}
int main() {
int *arr = generateOddSquare(3);
for (int i = 0; i < 9; i++) {
cout << arr[i] << "\n";
}
}
What is wrong with it? Is the way I'm declaring my pointers correct?
You create an array of pointers:
int **matrix = new int *[n]
but don't initialise those to point to anything; hence the segementation fault when you try to dereference them. If you really must juggle pointers, then allocate an array for each to point at:
for (int i = 0; i < n; ++i) {
matrix[i] = new int[n];
}
and don't forget to delete all of these allocations, if you care about memory leaks.
Unless this is an exercise in masochism, use the standard library to make life easier:
std::vector<std::vector<int>> matrix(n, std::vector<int>(n));
and return std::vector<int>
rather than int*
to save the caller the hassle of juggling and deleting the pointer.