Re: need help in dynamic memory alloction of multi-dimensional array

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



"Allen Maki" <allen_class@xxxxxxxxxxx> wrote in message
news:OHAzSzjcGHA.2068@xxxxxxxxxxxxxxxxxxxx
When I used the 2 following lines in a simple code, the code will
compile with no error:

long (*pprime)[max2];

pprime = new long[max][max2];

But if I separate the two lines by putting the first in a class
const int max = 5;
const int max2 = 4;
class Array
{
public:
long (*pprime)[max2];

Here max2 is a constant, with the value of 4.

Array(int max, int max2)
{
pprime = new long[max][max2];

Here max2 referes to a parameter passed to the constructor, not to the
constant as in the first case. You are attempting to allocate an array
of objects of type long[max2], but it's only a valid type when max2 is a
constant, which here it isn't.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


.