| | |
Returning a pointer for a 2d array
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 11
Reputation:
Solved Threads: 0
Hi I have a class MapMaker
I need a function that returns the "grid" attribute but I do not know how. Please help me. Below is the code that implements the logic for my function.
Thanks!
C++ Syntax (Toggle Plain Text)
class MapMaker { private: Node *grid[MAP_WIDTH][MAP_HEIGHT]; public: MapMaker(); ~MapMaker(); /*this does not work*/ Node*** getMap(int i); };
I need a function that returns the "grid" attribute but I do not know how. Please help me. Below is the code that implements the logic for my function.
C++ Syntax (Toggle Plain Text)
/*what to return here*/ MapMaker::getMap(int i) { for (int i = 0; i < MAP_HEIGHT; i++) { for (int j = 0; j < MAP_WIDTH; j++) { grid[j][i] = new Node(j, i); } } return grid; }
Thanks!
Last edited by racumin; Oct 26th, 2009 at 2:52 am.
0
#3 34 Days Ago
How about dynamically allocating your Node, then your return type
can be of Node***.
Alternative, you can use 3d vectors and have the return types as 3d
vectors as well.
can be of Node***.
Alternative, you can use 3d vectors and have the return types as 3d
vectors as well.
I give up! 1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ] 2) What does this sequence equal to : (.5u - .5a)(.5u-.5b)(.5u-.5c) ... 3) What is the 123456789 prime numer? Ask4Answer
•
•
Join Date: Oct 2009
Posts: 11
Reputation:
Solved Threads: 0
0
#4 34 Days Ago
So is there no other way but to follow firstPerson's suggestion? I just want to have a 2d array of Node pointers so that inside the class where I need to use this, I can just access it like an ordinary 2d array.
C++ Syntax (Toggle Plain Text)
Node *node_from_grid = grid[x][y];
0
#5 33 Days Ago
There are other alternatives like boost shared_ptr where
more than 1 pointer can have reference to an object.
I assume Node is a class or a struct, so have the user pass in
a Node *[MAP_WIDTH][MAP_HEIGHT] and from there you can copy the
original content onto the array that passed on, note what you are trying
to do might be expensive depending on the size of you 3d array.
more than 1 pointer can have reference to an object.
I assume Node is a class or a struct, so have the user pass in
a Node *[MAP_WIDTH][MAP_HEIGHT] and from there you can copy the
original content onto the array that passed on, note what you are trying
to do might be expensive depending on the size of you 3d array.
I give up! 1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ] 2) What does this sequence equal to : (.5u - .5a)(.5u-.5b)(.5u-.5c) ... 3) What is the 123456789 prime numer? Ask4Answer
•
•
Join Date: Oct 2009
Posts: 11
Reputation:
Solved Threads: 0
0
#6 32 Days Ago
Hi, Node is a class I made. I think I will use the
But I have problems initializing it. What is the correct syntax for initializing this so that I can access it like a 2d array? For example, I want to have a method like this:
Thanks!
C++ Syntax (Toggle Plain Text)
Node*** grid;
But I have problems initializing it. What is the correct syntax for initializing this so that I can access it like a 2d array? For example, I want to have a method like this:
C++ Syntax (Toggle Plain Text)
void myMethod(Node*** grid) { //calls the toString() method of Node[0][3] cout << grid[0][3]->toString() << endl; }
Thanks!
1
#7 32 Days Ago
Hven't tried it out and its about 3 am but I'll give it a shot :
I will build up :
CASE A : First to create a 1d array :
CASE B : Now to create a 2 d array you need to have each of the node
element carry some more node element.
CASE C: //For a 3d array each node has to has NODE_Y element and each
NODE_Y element has to have its NODE_Z element
For case A ( 1 D array ) you can access and write data like this :
Node[i] = ...
For case B (2d array) you can access and write data like this :
Node[i][j] = ...
For case C( 3d array) you can access and write data like this :
Node[i][j][k] = ...
In your original post you have :
which is case 3 but in the form of : *Node[x][y] which is the same as Node[x][y][0]
To delete the arrays you can do the following for each case, respectively ,
Case A :
Case B :
Case C :
I will build up :
CASE A : First to create a 1d array :
C++ Syntax (Toggle Plain Text)
const int NODE_SIZE = 5; Node * p = new Node[NODE_SIZE];
CASE B : Now to create a 2 d array you need to have each of the node
element carry some more node element.
C++ Syntax (Toggle Plain Text)
const int NODE_X = 5; const int NODE_Y = 5; Node **p = new Node*[NODE_X]; //each node_x needs its own node y to create a 5x5 2d array for(int i = 0; i < NODE_X; i++){ p[i] = new Node[NODE_Y]
CASE C: //For a 3d array each node has to has NODE_Y element and each
NODE_Y element has to have its NODE_Z element
C++ Syntax (Toggle Plain Text)
const int NODE_X = 5; const int NODE_Y = 5; const int NODE_Z = 5; //currentl 1d array Node*** p = new Node**[NODE_X]; //now a 2d array for(int i = 0; i < NODE_X; i++){ p[i] = new Node*[NODE_Y]; } //now a 3d array for(int i = 0; i < NODE_Y; i++) { for(int j = 0; j < NODE_Y; j++){ p[i][j] = new Node[NODE_Z]; } }
For case A ( 1 D array ) you can access and write data like this :
Node[i] = ...
For case B (2d array) you can access and write data like this :
Node[i][j] = ...
For case C( 3d array) you can access and write data like this :
Node[i][j][k] = ...
In your original post you have :
C++ Syntax (Toggle Plain Text)
Node *node_from_grid = grid[x][y];Node *node_from_grid = grid[x][y];
To delete the arrays you can do the following for each case, respectively ,
Case A :
C++ Syntax (Toggle Plain Text)
delete [] Node;
C++ Syntax (Toggle Plain Text)
for(int i = 0; i < NODE_X; i++) delete Node[i];
C++ Syntax (Toggle Plain Text)
for(int i = 0; i < NODE_Y; i++) { for(int j = 0; j < NODE_Y; j++){ p[i][j] = new Node[NODE_Z]; } }
I give up! 1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ] 2) What does this sequence equal to : (.5u - .5a)(.5u-.5b)(.5u-.5c) ... 3) What is the 123456789 prime numer? Ask4Answer
•
•
Join Date: Oct 2009
Posts: 11
Reputation:
Solved Threads: 0
1
#8 32 Days Ago
Thanks firstPerson! Your solution worked perfectly! So this is what I did. Please correct me if the following code has mistakes. My goal is to create a 2d array of Node pointers. So, I declared it as firstPerson suggested.
Then I initialize it using this:
Now, to delete the grid, I used this:
I'm pretty sure that this code has no memory leaks (right?) But I am not sure if my explanation (comments) was correct.
Once again, thank you very much!
C++ Syntax (Toggle Plain Text)
Node*** grid;
Then I initialize it using this:
C++ Syntax (Toggle Plain Text)
//initialize array for the width -> not so sure how this works grid = new Node**[MAP_WIDTH]; //initialize for the height -> not so sure how this works for (int i = 0; i < MAP_WIDTH; i++) { grid[i] = new Node*[MAP_HEIGHT]; } //create Node objects for (int i = 0; i < MAP_HEIGHT; i++) { for (int j = 0; j < MAP_WIDTH; j++) { grid[j][i] = new Node(j, i); } }
Now, to delete the grid, I used this:
C++ Syntax (Toggle Plain Text)
//delete first the Node objects for (int i = 0; i < MAP_HEIGHT; i++) { for (int j = 0; j < MAP_WIDTH; j++) { delete grid[j][i]; } } //delete the pointers pointing to the Node objects for (int i = 0; i < MAP_WIDTH; i++) { delete grid[i]; } //delete the actual array delete [] grid;
I'm pretty sure that this code has no memory leaks (right?) But I am not sure if my explanation (comments) was correct.
Once again, thank you very much!
![]() |
Similar Threads
- Return a pointer to the array C++ (C++)
- Return pointer to array of a struct (C++)
- Quick Question: Pointer to a 2D array (C++)
- Passing a pointer to an array. (C)
- returning three dimensional array (C)
- Pointer to array (C++)
- pointer and array arithmetic (C)
- Declaration of dynamic pointer array puzzle. (C)
- structures containing a pointer to an array (C++)
Other Threads in the C++ Forum
- Previous Thread: DivByZero exception handling help
- Next Thread: filestream && multidimensional arrays
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






