| | |
Returning a pointer for a 2d array
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
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 Oct 27th, 2009
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.
1) Prove that the area of a circle is pi*r^2, where "r" is the radius of the circle. 2) Problem 2[b]solved by : jonsca
•
•
Join Date: Oct 2009
Posts: 11
Reputation:
Solved Threads: 0
0
#4 Oct 27th, 2009
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 Oct 27th, 2009
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.
1) Prove that the area of a circle is pi*r^2, where "r" is the radius of the circle. 2) Problem 2[b]solved by : jonsca
•
•
Join Date: Oct 2009
Posts: 11
Reputation:
Solved Threads: 0
0
#6 Oct 28th, 2009
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 Oct 29th, 2009
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]; } }
1) Prove that the area of a circle is pi*r^2, where "r" is the radius of the circle. 2) Problem 2[b]solved by : jonsca
•
•
Join Date: Oct 2009
Posts: 11
Reputation:
Solved Threads: 0
1
#8 Oct 29th, 2009
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
Views: 569 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






