# Spiral matrix run time error !?

There is a strange runtime error occurring outside of implementation function, but I guess have got correct solution

``````int* spiralOrder(int** matrix, int matrixRowSize, int matrixColSize) {
int m = matrixRowSize;
int n = matrixColSize;

int size = matrixRowSize * matrixColSize;
int *result = malloc(size * sizeof(*result));
// for (int i = 0; i < size; i++) {
//     result[i] = 0;
// }

int k = 0;

for (int i = 0; i < m / 2 + 1; i++) {

int first = i;
int mlast = m - i - 1;
int nlast = n - i - 1;

for (int j = first; j <= nlast; j++) {
result[k++] = matrix[i][j];
}

for(int j = first + 1; j <= mlast; j++) {
result[k++] = matrix[j][nlast];
}

for (int j = nlast - 1; j >= first; j--) {
result[k++] = matrix[mlast][j];
}

for (int j = mlast - 1; j > first; j--) {
result[k++] = matrix[j][first];
}
}

for (int i = 0; i < size; i++) {
printf("%d ", result[i]);
}
printf("\n");

return &result;
}
``````

any suggestions?

• if first==mlast, say there is only one row in matrix, the first and third inner loop will duplicate the single row.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.