Archived

This topic is now archived and is closed to further replies.

Dynamic Declaring 2-Dimensional Arrays

This topic is 5316 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey folks, I''m having a little trouble dynamically declaring a 2-dimensional array. My code is something like what follows:
  
Prg_Rom_Page_Count = fgetc(RomImage); // Pull out of open file

Prg_Rom_Page_Size = 16384;
BYTE *Mapper_Regs = new byte[4];
BYTE *Prg_Rom_Data = new byte[Prg_Rom_Page_Count][Prg_Rom_Page_Size];
  
I''m have trouble with the line where I am declaring the doubly subscripted array. I know this is probably something stupid that I forgot to do, but if someone could help me out I''d appreciate it. Thanks.

Share this post


Link to post
Share on other sites
you have to do something like this:

#define ROWS 10
#define COLS 20

BYTE **array;

array = new BYTE*[ROWS] // create ROWS

for( int i = 0; i < 20; i++ )
array = new BYTE[COLS]; // create COLS

"- To begin with, said the Cat, a dog''s not mad. You grant that?
- I suppose so, said Alice.
- Well, then, - the Cat went on - you see, a dog growls when it''s angry, and wags its tail when it''s pleased. Now I growl when I''m pleased, and wag my tail when I''m angry. Therefore I''m mad."

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
This method works with malloc, alloc, calloc, new etc..

#include <iostream>
#include <stdlib.h>

using namespace std;

int main(int argc, char *argv[])
{
int **array = 0,
a = 30,
b = 30;

array = new int*Link

Share this post


Link to post
Share on other sites
int n;
char **p, temp[256];

p=(char**)malloc(sizeof(char*)*n);
for(i=0;i{
gets(temp);
p=(char*)malloc(strlen(temp)+1);
strcpy(p[i],temp);
}

[edited by - xxx_shinobi on May 28, 2003 3:51:47 PM]

Share this post


Link to post
Share on other sites
Just to clarify, here is how you would allocate and deallocate your two-dimensional array:

// allocation
BYTE **Prg_Rom_Data = new BYTE*[Prg_Rom_Page_Count];
for (int ix = 0; ix < Prg_Rom_Page_Count; ++ix)
    Prg_Rom_Data[ix] = new BYTE[Prg_Rom_Page_Size];

...
// do stuff with dynamically allocated memory
...

// deallocation
for (int ix = 0; ix < Prg_Rom_Page_Count; ++ix)
    delete [] Prg_Rom_Data[ix];
delete [] Prg_Rom_Data;


[ Google || Start Here || ACCU || MSDN || STL || GameCoding || BarrysWorld || E-Mail Me ]

Share this post


Link to post
Share on other sites