Amiga.org

Operating System Specific Discussions => Amiga OS => Amiga OS -- Development => Topic started by: Glaucus on November 13, 2002, 02:42:21 AM

Title: Need help with C++ array declarations!!!!
Post by: Glaucus on November 13, 2002, 02:42:21 AM
Please, someone educate me here!!!

I originally wrote some C code to allocate a matrix:

UBYTE (*BrickBuffer)[MAX_GRIDX][MAX_GRIDY];
BrickBuffer = AllocMem(sizeof(*BrickBuffer), 0L);

This works fine.  I then tried to make the above work using the new operator in C++:

UBYTE (*BrickBuffer)[MAX_GRIDX][MAX_GRIDY];
BrickBuffer = new(UBYTE[MAX_GRIDX][MAX_GRIDY]);

The above gets me this error is SAS6.58:
Src/LevelData.cpp 354 Error 1544: ( unsigned char (*)[58][41]) = ( unsigned char (*)[41]): Invalid type for binary operator.

Okay, why is it doing this to me?!!?  I obviously don't understand how the new() operator works here. So how does one dynamically allocate a matrix in C++ using new()?!?!?  Any suggestions???

  - Mike
Title: Re: Need help with C++ array declarations!!!!
Post by: CodeSmith on November 13, 2002, 03:10:39 AM
Your sample code is a bit confusing - are you allocating a 2D array of objects? if so, try this:

UBYTE **BrickBuffer;
int i;

BrickBuffer = new UBYTE*[MAX_GRIDX];
for (i = 0;i < MAX_GRIDX;i++)
    BrickBuffer = new UBYTE[MAX_GRIDY];

You can then use the array as usual, eg BrickBuffer
  • [y].doSomething();


when deleting the array, do it like this:

for (i = 0;i < MAX_GRIDX;i++)
    delete[ ] BrickBuffer;

delete[ ] BrickBuffer;

SAS/C implements an older version of the C++ standard, but I don't think even newer compilers support anything other than new, new[] and new().  
Title: Re: Need help with C++ array declarations!!!!
Post by: Glaucus on November 13, 2002, 07:12:11 AM
Okay thanks, I'll give that a shot tomorrow.

I always assumed that the new operator could be used to allocate a matrix (which is what I was attempting to do in the first place).  Now it seems you need to allocate each column seperatly.

  - Mike