Martin said:
Gavin said:
Martin wrote:
An unrelated problem is that when passing multidimensional arrays as
parameters to functions, it's necessary to specify the depths of all but
the first dimension. The compiler says that these must be constant (not
variables), but I need to specify the depths using variables which are
declared constant and whose values are known at compile time. Is this
possible?
Yes, you need to use compile time constants but they need not be
constant literals. Can you post a minimal code example that is causing
you a problem.
Have you got a good reason to rule out a vector of vectors [of vectors
...] in preference to raw arrays?
Gavin Deane
Thanks for your reply.
Code snippet:
------------------------------------------
double MC(double K,
double S[],
double v[],
double d[],
double r,
double exDates[],
const unsigned long DIMENSIONS,
const unsigned long GRIDPOINTS,
double frontiers[][DIMENSIONS][GRIDPOINTS], // Here's the problem
double gridStep[],
unsigned long evaldate,
unsigned long NumberOfPaths)
------------------------------------------
Ah, no you can't do that. DIMENSIONS and GRIDPOINTS are not
compile-time constants. Nothing tells the compiler what value they
have. You can do something like
const int size = 42;
void foo(int array2d[][size])
{
// ...
}
because the value of 'size' is known to the compiler when it encounters
foo. But that doesn't seem to be what you want.
As a style issue, avoid using ALL_CAPS for anything except macros
(which themselves should be avoided wherever possible) - and always use
ALL_CAPS when you must have a macro. That will protect you from having
your code stomped all over by the preprocessor.
The reason for not using vectors of vectors of vectors instead of
threedimensional arrays is that the code is intended for parallel
computing, currently using MPI, and a colleague encountered difficulties
sending vectors between nodes. Perhaps there's a way around that though.
Is it much easier to use nested vectors (I suppose you mean the Vector
class?) as function parameters, than multidimensional arrays?
Yes.
#include <vector>
typedef std::vector<std::vector<std::vector<int> > > vec3d;
void foo(const vec3d& v)
{
// ...
}
int main()
{
vec3d bar; // Use appropriate constructor to make the vectors the
right size.
foo(bar);
}
Also, any memory management and the associated exception safety is
taken care of for you.
This is my first program in C/C++.
There's no such language as C/C++. I hope that doesn't sound pedantic.
You may be well aware of what I am about to say, and by "C/C++" you may
mean "C or C++", in which case you can ignore the following. But there
is a common misconception that C and C++ are similar and that C++ is
just C expanded a bit, which leads people to say "C/C++" without
understanding the differences. C and C++ are very different. Some
things that are legal C are not legal C++. More importantly though,
within the common subset of the two languages, techniques regarded as
good practice in C are often porr C++.
It sounds like you may have a good reason to avoid std::vector in this
case, but it's generally good practice to use the standard library
tools in preference to hand written equivalents when you can. They are
designed to make your life easier and they achieve that.
Gavin Deane