Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with arrays (declared on the stack) in C++?

Tags:

c++

arrays

I have a class to parse a matrix that keeps the result in an array member:

class Parser
{
  ...
  double matrix_[4][4];
};

The user of this class needs to call an API function (as in, a function I have no control over, so I can't just change its interface to make things work more easily) that looks like this:

void api_func(const double matrix[4][4]);

The only way I have come up with for the caller to pass the array result to the function is by making the member public:

void myfunc()
{
  Parser parser;
  ...
  api_func(parser.matrix_);
}

Is this the only way to do things? I'm astounded by how inflexible multidimensional arrays declared like this are. I thought matrix_ would essentially be the same as a double** and I could cast (safely) between the two. As it turns out, I can't even find an unsafe way to cast between the things. Say I add an accessor to the Parser class:

void* Parser::getMatrix()
{
  return (void*)matrix_;
}

This will compile, but I can't use it, because there doesn't seem to be a way to cast back to the weirdo array type:

  // A smorgasbord of syntax errors...
  api_func((double[][])parser.getMatrix());
  api_func((double[4][4])parser.getMatrix());
  api_func((double**)parser.getMatrix()); // cast works but it's to the wrong type

The error is:

error C2440: 'type cast' : cannot convert from 'void *' to 'const double [4][4]'

...with an intriguing addendum:

There are no conversions to array types, although there are conversions to references or pointers to arrays

I can't determine how to cast to a reference or pointer to array either, albeit that it probably won't help me here.

To be sure, at this point the matter is purely academic, as the void* casts are hardly cleaner than a single class member left public!

like image 456
Owen Avatar asked Sep 10 '08 19:09

Owen


People also ask

Are arrays allocated on stack in C?

Unlike Java, C++ arrays can be allocated on the stack.

Is array stored in stack or heap?

Storage of Arrays As discussed, the reference types in Java are stored in heap area. Since arrays are reference types (we can create them using the new keyword) these are also stored in heap area.

What happens when array is declared in C?

Array Declaration by Initializing Elements An array can be initialized at the time of its declaration. In this method of array declaration, the compiler will allocate an array of size equal to the number of the array elements. The following syntax can be used to declare and initialize an array at the same time.


5 Answers

Here's a nice, clean way:

class Parser
{
public:
   typedef double matrix[4][4];

   // ...

   const matrix& getMatrix() const
   {
      return matrix_;
   }

   // ...

private:
  matrix matrix_;
};

Now you're working with a descriptive type name rather than an array, but since it's a typedef the compiler will still allow passing it to the unchangeable API function that takes the base type.

like image 175
Shog9 Avatar answered Oct 01 '22 03:10

Shog9


Try this. It compiles cleanly on gcc 4.1.3:

typedef double FourSquare[4][4];

class Parser
{
  private:
    double matrix_[4][4];

  public:
    Parser()
    {
        for(int i=0; i<4; i++)
          for(int j=0; j<4; j++)
            matrix_[i][j] = i*j;
    }

  public:
    const FourSquare& GetMatrix()
    {
        return matrix_;
    }
};

void api_func( const double matrix[4][4] )
{
}

int main( int argc, char** argv )
{
    Parser parser;
    api_func( parser.GetMatrix() );
    return 0;
}
like image 32
Andrew Avatar answered Oct 01 '22 03:10

Andrew


I've used a union like this to pass around matrices in the past:

union matrix {
    double dflat[16];
    double dmatr[4][4];
};

Then pass a pointer in to your setter and copy the data into the matrix in your class.

There are ways of handling this otherwise (that are more generic), but this solution tends to be the cleanest in the end, in my experience.

like image 23
Serafina Brocious Avatar answered Oct 01 '22 03:10

Serafina Brocious


I thought matrix_ would essentially be the same as a double**

In C there are true multi-dimensional arrays, not arrays of pointers to arrays, so a double[4][4] is a contiguous array of four double[4] arrays, equivalent to a double[16], not a (double*)[4].

There are no conversions to array types, although there are conversions to references or pointers to arrays Casting a value to a double[4][4] would attempt to construct one on the stack - equivalent to std::string(parser.getMatrix()) - except that the array doesn't supply a suitable constructor. You probably did't want to do that, even if you could.

Since the type encodes the stride, you need a full type (double[][] won't do). You can reinterpret cast the void* to ((double[4][4])*), and then take the reference. But it's easiest to typedef the matrix and return a reference of the correct type in the first place:

typedef double matrix_t[4][4];

class Parser
{
    double matrix_[4][4];
public:
    void* get_matrix () { return static_cast<void*>(matrix_); }

    const matrix_t& get_matrix_ref () const { return matrix_; }
};

int main ()
{
    Parser p;

    matrix_t& data1 = *reinterpret_cast<matrix_t*>(p.get_matrix());

    const matrix_t& data2 = p.get_matrix_ref();
}
like image 22
Pete Kirkham Avatar answered Oct 01 '22 03:10

Pete Kirkham


To elaborate on the selected answer, observe this line

const matrix& getMatrix() const

This is great, you don't have to worry about pointers and casting. You're returning a reference to the underlying matrix object. IMHO references are one of the best features of C++, which I miss when coding in straight C.

If you're not familiar with the difference between references and pointers in C++, read this

At any rate, you do have to be aware that if the Parser object which actually owns the underlying matrix object goes out of scope, any code which tries to access the matrix via that reference will now be referencing an out-of-scope object, and you'll crash.

like image 42
Orion Edwards Avatar answered Oct 01 '22 03:10

Orion Edwards