Returning an array from a function

Hi everyone,

I'm trying to return the size of a matrix (i.e. something which has the form [A.height, A.width], so it's an array) from a function but C++ won't let me.

I gather that you can't simply return arrays and that pointers must be used. Could anybody show me the easiest way to do this? Here's my function

1
2
3
4
5
6
7
8
9
10
int size(matrixdouble_new &A)
{
    // We define an array to contain [height(A), width(A)], i.e. the size of A
    int size_A [2];
    
    size_A[0] = A.height;
    size_A[1] = A.width;
     
  return size_A;
}


Thanks!
This this one way to do this:

1
2
3
4
5
6
7
8
9
10
//function definition:
void size(matrixdouble_new &A, int size_A[])
{
    size_A[0] = A.height;
    size_A[1] = A.width;
}

//function use:
int sz[2];
size(matrix, sz);
Thanks melkiy, that's helpful.

Ideally, I'd like to use my function in the following way (a la Matlab, with just the matrix being inputted):

1
2
3
//function use:
int size_array[2];
size_array = size(matrix);


Any ideas?

- Returning a pointer pointing to a local object is hazardous.
- I think of creating size_A on the heap then return the pointer pointing to size_A[0], but allocating memory in one function while freeing it in another can be dangerous.
- On line 3 it looks like in the "pointer version" of this you are going to assign a pointer to another, but if you call delete on one later on, you would create a stray pointer...

- You may create the pointer in the calling function, then pass the pointer to the function (similar to what melkiy did), but you have to also pass in the pointer...

There may be better ways...;)
Last edited on
You are trying to do something that doesn't make much sense in C++. It is better if you make yourself a matrix class, which will provide you with information about its size, etc.

Can you be more specific about the matrixdouble_new type?

[edit] Argh. I just found your other thread (which I don't want to read). Your matrix class should be able to report its size. Here is an example to get you started:
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
template <typename ValueType>
class matrix_t
  {
  public:
    typedef ValueType value_type;

    unsigned rows()    const { return f_rows;    }
    unsigned columns() const { return f_columns; }

    matrix_t( unsigned rows = 0, unsigned columns = 0 ):
      f_rows( 0 ),
      f_columns( 0 ),
      f_values( NULL )
      {
      resize( rows, columns );
      }

   ~matrix_t()
      {
      if (f_values) delete [] f_values;
      }

    matrix_t& resize( unsigned rows, unsigned columns )
      {
      ...
      return *this;
      }

    // etc

  private:
    unsigned    f_rows;
    unsigned    f_columns;
    value_type* f_values;
  }

Now you can get the size:
1
2
3
4
5
6
7
  matrix_t <double> m( 4, 3 );

  cout << "The size of my matrix is ("
       << m.rows()
       << ","
       << m.columns()
       << ").\n";

Good luck.

[edit] added resize() method prototype to avoid future questions... LOL [/edit]
Last edited on
Thanks Duoas, that's a lot more sensible way to do it.

Mike
Topic archived. No new replies allowed.