Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Need help with a segmentation fault

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
stuarte9



Joined: Mon Mar 08, 2010 5:03 pm
Posts: 46
Location: Scotland

PostPosted: Thu Jan 12, 2012 2:37 pm    Post subject: Need help with a segmentation fault Reply with quote

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:
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
Back to top
View user's profile Send private message
stuarte9



Joined: Mon Mar 08, 2010 5:03 pm
Posts: 46
Location: Scotland

PostPosted: Fri Jan 13, 2012 2:27 pm    Post subject: Solved! Reply with quote

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
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast