# Thread: 2d array of object pointers

1. ## 2d array of object pointers

I have a 2d array of object pointers. It looks like this.

Code:
`someObject *arr[8][8];`
This arr array is a member of another class. I tried initializing it in a double for loop by pointing all of the positions to a default "someObject".

Code:
```SomeObject *s = new SomeObject();
for(int i=0; i < 8; i++){
for (int j=0; j < 8; j++){
arr[i][j] = SomeObject;
}
}```
The code compiled without error but doesn't run. It doesn't give any error messages either. I guess some memory is being overwritten. Am I doing this wrong? I want arr to be a 2d array of SomeObject pointers so that I can do things like arr[x][y]->func().

2. I believe you can just write:
Code:
```for (unsigned int i=0; i < 8; i++){
for (unsigned int j=0; j < 8; j++){
*arr[i][j] = SomeObject();
}
}```

3. wont that create 64 separate objects? I'd rather have each spot point to one object.

4. I'd rather have each spot point to one object.
Ah, sorry about that. More like:

Code:
```for (unsigned int i=0; i < 8; i++){
for (unsigned int j=0; j < 8; j++){
arr[i][j] = new SomeObject;
}
}```
Of course you have to delete accordingly.

EDIT:
I wonder if you can consider this instead:
Code:
```//SomeObject* arr[8];
for (unsigned int i = 0; i < 8; ++i) {
arr[i] = new SomeObject[8];
}```
But there's the issue that it does not have to be an m*n 2D array anymore, but could become an array of 8 arrays of different sizes, at some point later in the code.

5. that works, but doesn't it still create 64 new objects? Can I have all my array slots point to one object?

Also now I have a problem calling member functions. For instance arr[x][y]->getChar() (which for this example should just return some member char of the SomeObject class) returns some different values than what the objects were initialized with. Why would that happen?

EDIT:
I think it would be best to keep it as an arr[8][8]. I'm trying to program a chess game to learn c++ and arr is the board. Of course I'm not calling it arr in my code

6. >>Can I have all my array slots point to one object?

Yes. Just create an object, and have all your array indices point to it. (I think that's correct, anyway. Not sure if it would actually run, mind you ).

Code:
```SomeObject *mainObj = new SomeObject();
for(unsigned int i=0; i<8; i++){
for(unsigned int j=0; j<8; j++){
arr[i][j] = mainObj;
}
}```
I think I may be completely missing the point though. Wouldn't be the first time.

7. I have a better idea. Eliminate the 2nd dimension of the array. Why have a 2D array of pointers to objects? Why?
There are enough challenges and problems in programming w/o creating your own to deal with.

8. psycopath, I thought that would work, but my program crashes when I do that.
Bubba, I'm new to c++ and didn't think a 2d array of pointers would be challenging. I thought that the best way to reference objects in 2d space would be a 2d array. The 2d array is a chess board where each spot holds a pointer to a game piece. Before I just had it as a 2d char array where each spot had a character representation of the piece on it, but I like the pointer idea better. Is there a better way?

9. >> The 2d array is a chess board where each spot holds a pointer to a game piece.

If each spot holds a game piece, then you cannot have each spot in the array hold a pointer to the same object. Each game piece is different, so each entry in the 2D array should be different. Given that, I think a SomeObject[8][8] might be the best idea, no pointers or dynamic memory necessary. Of course, that depends on how your pieces will interact and how they will move around.

10. I actually just planned on initializing it to have every spot point to the same object. This object is a special game piece that represents a spot with no game piece. Later when I put pieces on the board, the board pointers would point to whatever gamepiece is on that spot. Then when a player moves a piece, I can just check to see if a piece is on the square they moved to rather than iterating through each players pieces to see if any locations match up.

Also I'm trying to make everything be light on memory space, so I think SomeObject[8][8] would be a lot bigger than SomeObject*[8][8]. I'm planning on implementing a minmax AI and I don't want to run out of memory. I may be wrong about this though. I figure that the minmax tree will need some board configuration at each node, and if the board is full of actual objects, then memory would run out quickly. If the board was just pointers, then I wouldn't need to create nearly as many objects.

11. In that case, I can see using pointers for this situation. Just make sure you have some other code that manages the memory of those pointers. For example, you could have a PieceManager class that creates all 33 pieces (or however you do it) and then provides an interface so that the rest of the program can get the appropriate pointer when they want it. That manager class would then clean up the pieces in its destructor when the game is over. You would then initalize each empty spot in the board with the pointer for an empty piece returned by the manager class.

You don't want the only copy of the pointer to be the one inside the board, or you might leak the memory or have other issues keeping track of it.

12. Thats a good idea, I'll probably do something like that.
Shouldn't this still work though?

SomeObject *s = new SomeObject();
for(int i=0; i < 8; i++){
for (int j=0; j < 8; j++){
arr[i][j] = SomeObject;
}
}
My program crashes and gives no errors. Is this what is meant by "undefined behavior"? Could it be a memory issue?

13. It should be
Code:
`arr[i][j] = s;`

14. That example had a typo. I think my code had the same typo though :-/. Either that or I'm not deleting properly and have a bunch of leaked memory. Thanks for your help.

15. >> Either that or I'm not deleting properly and have a bunch of leaked memory.
That's why I'd suggest making a single class that handles the new and delete parts in the constructor and destructor, then as long as you create your instance of that class before you use the pointers and don't let it go out of scope until you are done, you don't have to worry about memory management in the rest of the code.