
-----------------------------------
HazySmoke)345
Thu Sep 14, 2006 7:06 pm

Passing 2D array by reference
-----------------------------------
Well, yes. I know how to pass a 1D array by reference. I can just use a pointer for the first element and I'm still able to refer to its elements by using the void someFunction(int** variable)
  { variable[3][5] = 1; }

And the program crashed.

-----------------------------------
wtd
Thu Sep 14, 2006 11:14 pm


-----------------------------------
Was the array you passed in properly allocated?

-----------------------------------
OneOffDriveByPoster
Fri Sep 15, 2006 8:05 am

Re: Passing 2D array by reference
-----------------------------------
Well, yes. I know how to pass a 1D array by reference. I can just use a pointer for the first element and I'm still able to refer to its elements by using the void someFunction(int** variable)
  { variable[3][5] = 1; }

And the program crashed.

Use int **var with dynamically allocated 2-D arrays (where it is a pointer-to pointer-to-int).
I suspect the 2-D array you are passing is declared like so:  int v[X][Y]; (something else).

Properly-allocated multidimenional arrays of both types will give unexpected results if you pass them wrong.

-----------------------------------
wtd
Fri Sep 15, 2006 1:09 pm


-----------------------------------
Problems with multi-dimensional arrays will eventually teach you to love code like the following.

#include 
#include 

using namespace std;

template 
class Matrix2D
{
   private:
      size_t m_rows, m_cols;
      T *m_data; 
	  
	  size_t max_string_length() const;

   public:
      explicit Matrix2D();
      explicit Matrix2D(size_t rows, size_t cols);
      explicit Matrix2D(size_t rows, size_t cols, T initial_value);
	  Matrix2D(const Matrix2D& m);

	  Matrix2D& operator=(const Matrix2D& m);
	  
      virtual ~Matrix2D()
      {
         delete [] m_data;
      }      

      T &operator()(size_t row, size_t col);
      T operator()(size_t row, size_t col) const;
	  
	  template 
	  friend ostream& operator