Welcome, Guest. Please login or register.
Amiga Kit Amiga Store Iridium Banner AMIStore App Store A1200/A600 4xIDE Interface

AuthorTopic: Need help with C++ array declarations!!!!  (Read 1179 times)

0 Members and 1 Guest are viewing this topic.

Offline Glaucus

Need help with C++ array declarations!!!!
« 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
YOU ARE NOT IMMUNE
 

Offline CodeSmith

Re: Need help with C++ array declarations!!!!
« Reply #1 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().  
 

Offline Glaucus

Re: Need help with C++ array declarations!!!!
« Reply #2 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
YOU ARE NOT IMMUNE