are multi-demensional arrays on the heap impossible?
try this out on your compiler. get an error dev 4.9.4.1
my error says Cannot assign to int * from int[*][2] or something very similarCode:int * pInt = new int[2][2];
delete pInt;
Printable View
are multi-demensional arrays on the heap impossible?
try this out on your compiler. get an error dev 4.9.4.1
my error says Cannot assign to int * from int[*][2] or something very similarCode:int * pInt = new int[2][2];
delete pInt;
Not impossible
In both these functions, you can use the allocated array as p[row][col]Code:const int max_cols=20;
// use this when rows and columns are truly dynamic
// like int arr[x][y]
void f1 ( int num_rows, int num_cols ) {
int **p;
int i;
p = new int*[num_rows];
for ( i = 0 ; i < num_rows; i++ ) p[i] = new int[num_cols];
// use it
for ( i = 0 ; i < num_rows ; i++ ) delete p[i];
delete p;
}
// use this if colums is a compile time constant
// like int arr[x][10];
void f2 ( int num_rows ) {
int (*p)[max_cols];
p = new int[num_rows][max_cols];
// use it
delete p;
}
mm that looks very confusing. is there a simpler way? why can't i just type
Code:int * pInt = new int[2][2];
> is there a simpler way?
You mean like my f2 example
int * pInt = new int[2][2];
would be
int (*pInt)[2] = new int[2][2];
> why can't i just type
Because this is for a 1D array of int
int * pInt = new int[2];
You can't make it into a 2D array simply by changing the new operator.
> that looks very confusing
Then I suggest careful study here
Pointer declarations, especially the more complex ones are one of the tricker aspects of C and C++ programming.
I know the exact size of the array in the program (its going to be a 20x20 array of ROOMS for a text rpg, but i used ints for simplicity), so can I do this?..
the link your gave is for C, and i dont recognize most of the printfs and all that stuff..Code:ROOM * create() // so i don't need this parameter?
{
ROOM * pRoom[20]; //an array of pointers?
pRoom = new ROOM[20][20]; // replace with numbers?
// can i use pRoom as it were a regular pointer to an array now?
// such as pRoom[2][3]->method();
return pRoom;
}
could you explain why i needed to declare pRoom as an array of 20 ROOMS?
Code:#include <iostream>
class Room{ //Your class.....whatever.....
public:
int member;
};
int main(void)
{
Room** room; //pointer to a pointer to Room
int x,y;
/*Set up your array*/
room = new Room*[20];//Set first dimensions
for(x = 0;x < 20;x++)
room[x] = new Room[20];//Set second dimensions
/*Do whatever with it*/
for(x = 0;x < 20;x++)
for(y = 0;y < 20;y++)
room[x][y].member = x+y; //set values...blah blah
for(x = 0;x < 20;x++)
for(y = 0;y < 20;y++)
std::cout << room[x][y].member << " ";//Show results
/* Dont forget to clean up!*/
for(x = 0;x < 20;x++)
delete [] room[x];
delete [] room;
return 0;
}
> I know the exact size of the array in the program
So why are you even trying to allocate it when you can just say
ROOM rooms[20][20];
that would declare it either globally or on the stack - which i don't want. i'd like it on the heap.
i was wondering if you could explain a bit more of how that code works fordy, i'm reluctant to use code that i don't understandQuote:
Originally posted by Fordy
Code:#include <iostream>
class Room{ //Your class.....whatever.....
public:
int member;
};
int main(void)
{
Room** room; //pointer to a pointer to Room
int x,y;
/*Set up your array*/
room = new Room*[20];//Set first dimensions
for(x = 0;x < 20;x++)
room[x] = new Room[20];//Set second dimensions
/*Do whatever with it*/
for(x = 0;x < 20;x++)
for(y = 0;y < 20;y++)
room[x][y].member = x+y; //set values...blah blah
for(x = 0;x < 20;x++)
for(y = 0;y < 20;y++)
std::cout << room[x][y].member << " ";//Show results
/* Dont forget to clean up!*/
for(x = 0;x < 20;x++)
delete [] room[x];
delete [] room;
return 0;
}
Then
ROOM (*rooms)[20] = new ROOM[20][20];
Is all you need to get you
rooms[0][0] to rooms[19][19]
salem, are you saying i can skip that other code and just use yours & it will work?
From Fordy's code above:
This is to allocate space for 20 Room pointers. (20 Room*'s)Code:
/*Set up your array*/
room = new Room*[20];//Set first dimensions
Creating 20 Room objects on the heap, using the 20 pointers allocated initially.Code:
for(x = 0;x < 20;x++)
room[x] = new Room[20]; //Set second dimensions
First delete each object, then delete the initial pointer.Code:
/* Dont forget to clean up!*/
for(x = 0;x < 20;x++)
delete [] room[x];
delete [] room;
somehow that makes no sense to me.
room holds the address to the first of 20 pointers to ROOMs, which is stored on the heap
then each of those 20 pointers is set up to hold the address of a particular ROOM which is created on the heap again (so far, only 20 ROOMS have been made, not 400)
then you delete all the pointers.
it seems this only declares a single-dimensioned array, not a 2-D array like i'm trying to do am i crazy?
>>it seems this only declares a single-dimensioned array, not a 2-D array like i'm trying to do am i crazy?
You're right there!
Do you want a 2D array of rooms on the heap?
If you do, then you'll need to do something like this:
ROOM **rooms[2];
Then treat each element as in the original code.
What does that do? Not sure I understand how/why pointers to pointers are used.Quote:
ROOM **rooms[2];
What original code?? Everyone has a different idea on how to do it and im not sure how each one works, so im still looking for how to do it.