Welcome, Guest. Please login or register.

Author Topic: Need help with C++ array declarations!!!!  (Read 8894 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline GlaucusTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 4518
    • Show only replies by Glaucus
    • http://members.shaw.ca/mveroukis/
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

  • Sr. Member
  • ****
  • Join Date: Sep 2002
  • Posts: 499
    • Show only replies by 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 GlaucusTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 4518
    • Show only replies by Glaucus
    • http://members.shaw.ca/mveroukis/
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