Need help with a segmentation fault

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

Need help with a segmentation fault

Postby stuarte9 » Thu Jan 12, 2012 2:37 pm

Hi all,

I'm teaching myself C++ and getting a segmentation fault with my latest small prog. The fault occurs within the following function, at the "std::cin" statement.

Code: Select all
void getValues(int** theMatrix, int& theRows, int& theColumns)
{
   for (int row = 0; row < theRows; ++row)
   {
      // prompt for values
      std::cout << std::endl << "Enter values for row " << (row + 1) << std::endl;
   
      // and then use a for loop to assign values from cin
      for (int column = 0; column < theColumns; ++column)
      {
         std::cin >> theMatrix[row][column];
      }
   }
}



The intention of this function is to prompt for and read integer values into a 2D array.

Could someone please explain why I'm getting this fault as everything seems OK to me.

Thanks in advance,

Stuart
stuarte9
 
Posts: 58
Joined: Mon Mar 08, 2010 5:03 pm
Location: Scotland

Solved!

Postby stuarte9 » Fri Jan 13, 2012 2:27 pm

Hi all,

Just a quick note to say I've now solved the problem outlined in my original posting. It boiled down to not putting an ampersand (&) at the beginning of variable "theMatrix" in the function header.

Stuart
stuarte9
 
Posts: 58
Joined: Mon Mar 08, 2010 5:03 pm
Location: Scotland


Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests