Thread: 2d array of object pointers

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    10

    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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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();
    	}
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    10
    wont that create 64 separate objects? I'd rather have each spot point to one object.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Last edited by laserlight; 07-18-2006 at 10:17 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    10
    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
    Last edited by Potterd64; 07-18-2006 at 10:25 PM.

  6. #6
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    >>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.
    Last edited by psychopath; 07-18-2006 at 11:04 PM.
    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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. #8
    Registered User
    Join Date
    Jun 2006
    Posts
    10
    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. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 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. #10
    Registered User
    Join Date
    Jun 2006
    Posts
    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. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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. #12
    Registered User
    Join Date
    Jun 2006
    Posts
    10
    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. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It should be
    Code:
    arr[i][j] = s;

  14. #14
    Registered User
    Join Date
    Jun 2006
    Posts
    10
    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. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. 2D array pointers (almost got it)
    By tikelele in forum C Programming
    Replies: 4
    Last Post: 11-08-2007, 07:59 AM
  3. Array of Pointers + Deleting An Object = Problems
    By Nereus in forum C++ Programming
    Replies: 3
    Last Post: 03-04-2004, 12:16 PM
  4. Creating an array of object pointers
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2001, 10:01 PM
  5. Replies: 4
    Last Post: 09-12-2001, 02:05 PM