Segmentation Fault - Passing 2D Array to Function

I am trying to write a program that allows a user to choose the size of a matrix and uses a function to allow the user to input values into an array based on the chosen size. My program compiles but then I get a segmentation fault core dumped error after entering the first value for the matrix.

Any insight would be much appreciated.


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
//main program

#include "readMatrix.hpp"
#include <iostream>

int main()
{
   int matrixSize;                      //variable to represent size of matrix
   int **userMatrix;                    //pointer to pointer to create a dynamic 2D array

   //Prompts user to enter desired matrix size
   std::cout << "\nThis program will allow you to create a 2x2 or a 3x3 matrix.\n";
   std::cout << "Please enter 2 to create a 2x2 matrix.\n";
   std::cout << "Please enter 3 to create a 3x3 matrix.\n";
   std::cin >> matrixSize;
   //Validates input
   while ((matrixSize < 2) || (matrixSize > 3))
   {
        std::cout << "Please enter 2 or 3.\n";
        std::cin >> matrixSize;
   }

   //Creates matrix based on user choice of size
   userMatrix = new int *[matrixSize];                  //Creates the rows of the matrix
   for (int mRow = 0; mRow < matrixSize; mRow++)
   {
        userMatrix[matrixSize] = new int [matrixSize];  //Creates the columns of the matrix
   }

   //Calls readMatrix to allow user to enter values into matrix
   readMatrix(userMatrix, matrixSize);

   //Displays matrix on screen
   std::cout << "\nHere is the matrix you created.\n";
   for (int row = 0; row < matrixSize; row++)
   {
        std::cout << std::endl;                         //Creates a new display line for each row
        for (int col = 0; col < matrixSize; col++)
        {
           std::cout << userMatrix[row][col];           //Loops to display the value of each column in the row
        }
   }

   //Frees dynamically allocated memory
   delete [] userMatrix;
   userMatrix = 0;

   return 0;
}

//readMatrix header

#ifndef READMATRIX_HPP
#define READMATRIX_HPP

void readMatrix(int **, int);

#endif


//readMatrix implementation

#include "readMatrix.hpp"
#include <iostream>

/*************************************************
 *                                              *
 *              readMatrix                      *
 *                                              *
 * This function prompts a user to fill in      *
 * values of a matrix and stores these values   *
 * in a dynamically allocated 2D array.         *
 *                                              *
 * Accepts: pointer to 2D array                 *
 *          integer (size of matrix square)     *
 *                                              *
 * Returns: nothing (void)                      *
 *                                              *
*************************************************/
void readMatrix(int **matrix, int size)
{
   //Provides instructions for the user
   std::cout << "\nYou may now enter values into the matrix.\n";
   std::cout << "Values will be entered by row from left to right.\n";
   std::cout << std::endl;

   //Loops by row to allow user to enter values into the matrix
   for (int r = 0; r < size; r++)
   {
        std::cout << "Please enter row " << r+1 << " values:\n";
        for(int c = 0; c < size; c++)
        {
           std::cout << "Value " << (c+1) << ":  ";
           std::cin >> *(*(matrix + r) +c);
        }
   }
}

Last edited on
Line 27:
 
    userMatrix[matrixSize] = new int [matrixSize];  // Creates the columns of the matrix 
should be
 
    userMatrix[mRow] = new int [matrixSize];  // Creates the columns of the matrix 

That should fix the problem.

Also, when releasing the allocated memory at the end of main(), you need to delete each row of the array too.
1
2
3
4
5
   for (int mRow = 0; mRow < matrixSize; mRow++)
   {
        delete [] userMatrix[mRow];  // frees the columns of the matrix
   }
   delete [] userMatrix;


As a matter of style, this line in function readMatrix() is ok
 
   std::cin >> *(*(matrix + r) +c);
but it would be more readable like this:
 
   std::cin >> matrix[r][c];


Thank you so much Chervil! That fixed it! :)
Topic archived. No new replies allowed.