The problems.
This code from the question, …
x = calloc(10,sizeof(complex <double> *));
is ungood for two reasons:
unlike C, C++ does not have an implicit conversion from void*
, and
creating a jagged array by means of C++ new
is bad enough, doing it with the C level allocation functions is general an abomination (except where required by some function that one needs to call).
The first bullet point is why it fails to compile in C++.
What to do.
A purely technical fix is to use a cast, and then preferably a C++ named cast such as static_cast
or reinterpret_cast
. Both work fine in practice for conversion from void*
. However, in my opinion reinterpret_cast
expresses the conceptual operation more correctly, so I’d choose that – if I chose to cast.
A slightly better solution is to use C++ new
instead of C calloc
. With new
you have to add an empty parenthesis at the end to to get the guaranteed zero-initialization of calloc
. But the main problem is still that it's every low level and difficult (much work) to get right.
A much better general solution is to use a C++ container class.
The standard library offers std::vector
, with which the jagged array can be constructed like this:
typedef complex<double> Complex;
typedef vector<Complex> ComplexVec;
typedef vector<ComplexVec> ComplexVec2D;
ComplexVec2D x( 10 );
Then each ComplexVec
can just be resized, since std::vector
is a dynamically sized array.
What to do if you really want a MATRIX.
If you really want a matrix, i.e. an array of arrays of equal length, then instead of a jagged array consider a C++ class which just provides 2D indexing into a single vector.
In C++03-style it can go like this (off the cuff code):
typedef complex<double> Complex;
class ComplexMatric
{
private:
vector<Complex> items_;
int width_;
public:
Complex& operator()( int x, int y )
{ return items_[y*width_ + x]; }
Complex const& operator()( int x, int y ) const
{ return items_[y*width_ + x]; }
ComplexMatrix( int w, int h )
: items_( w*h )
, width_( w )
{}
};
And you'd use it like this:
ComplexMatrix x( 10, some_height );
This includes doing proper allocation and deallocation, and even supporting copying by assignment.
Disclaimer: code untouched by compiler's hands.