Ander Biguri
Ander Biguri

Reputation: 35525

Visual Studio 2008 debugging visualizing matrix

I am building a complex mathemathical software and I have to see while debugging some 500x700 matrixes that are created. Also the matrixes are not filled in order, meaning that "randomly" they are been filled and I need to see that. I've read that with the memory screen I can see the memory locations and their values, but with such huge matrixes the memory windows is not useful for me.

so my question is, is there any other way to debug adn watch matrixes in visual studio 08 than the memory window?

Upvotes: 2

Views: 795

Answers (1)

the_mandrill
the_mandrill

Reputation: 30842

The method I use to visualise complex data structures is to create a Dump() method in your class which formats the data into a string and returns a std::string. If you want to inspect the variable then select it in the debugger and invoke QuickWatch with Ctrl-Alt-Q and type myVariable.Dump(). This will then show you the string condensed into a single line. If you then click the magnifying glass icon it'll open up a text visualiser window that can be resized.

If you need more control over the format then make the Dump() method take an int for the level of detail you require.

EDIT:

Ok, here's an example, using stringstream to assemble a string: [disclaimer: I haven't tried compiling this]

class Matrix {
public:
  int m_Data[ROWS][COLS];

  ...
  std::string Dump() const {
    std::ostringstream oss;
    for (int r=0;r<ROWS; r++) {
      for (int c=0;c<COLS; c++) {      
        oss << m_Data[r][c] << " ";
      }
      oss << "\n";
    }
    return oss.str();
  }

  void DumpToFile() {
    std::ofstream os("output.txt");
    os << Dump();
  }
};

The Dump() method will output to a string that you can display in the QuickWatch window. If the text preview is too small then you could call DumpToFile() instead which will write the matrix to a file that you can view in a separate text editor. The key thing is that QuickWatch will evaluate function calls (to a limited degree) so you can use it to call these helper methods.

Upvotes: 2

Related Questions