0

Hi i would like to know how i could copy the contents of a 2d Array pointer in C++ to another location and set another pointer to it so that when i make changes on the copied pointer nothing happens to the original data?

Basically its an array pointer to pieces on a chessboard. so it goes like Piece * oldpointer = board[8][8]. now i want to copy all the contents in this pointer including methods like getvalue(), getcolor() etc which are in the Pieces header file to another location and set a pointer to it so i can do operations there and test it without it having to affect this orginal data? I read somewhere i had to use allocate() but im not sure. please help

menjaraz
  • 7,551
  • 4
  • 41
  • 81
GeeKaush
  • 33
  • 1
  • 6
  • A solution is discussed [here](http://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c/4810676#4810676), look under "Conversions" (about 3 pages down). – fredoverflow May 11 '11 at 11:38

3 Answers3

1

In C++ you could define 2D array type as follows (you need modern C++ compiler):

#include <array>
typedef std::array<std::array<Piece, 8>, 8> board_t;

If your compiler doesn't support std::array you can use boost::array instead:

#include <boost/array.hpp>
typedef boost::array<boost::array<Piece, 8>, 8> board_t;

Now you can use the type above. As I can see you need to copy the object to which the pointer points:

board_t* oldpointer = new board_t;

// do some with oldpointer

// now make a copy of the instance of the object oldpointer points to
// using copy-constructor
board_t* newpointer = new board_t( *oldpointer );
// now newpointer points to the newly created independent copy

// do more

// clean up
delete oldpointer;

// do more with newpointer

// clean up
delete newpointer;
Kirill V. Lyadvinsky
  • 97,037
  • 24
  • 136
  • 212
  • There's also std::vector, which is supported everywhere. – StilesCrisis May 11 '11 at 05:23
  • `std::vector` allocates memory dynamically. In chess there's fixed size of the board as I know, so no need in `std::vector`. – Kirill V. Lyadvinsky May 11 '11 at 07:22
  • You're right, but given that the OP is clearly a novice, pointing him towards high-level or newly-introduced libraries like Boost or TR1 may not be the best way for him to learn. I suspect a vector would be more than sufficient for his purposes and it's documented well and available everywhere. – StilesCrisis Jul 26 '11 at 00:11
1

Since you're using C++, why not define a copy constructor for your Piece class? Then just

Piece copied_piece(*board[8][8]);

If your class is POD, you should even be able to get by with the default copy constructor.

Djavu
  • 31
  • 1
  • 3
  • Although the board is what he wants to copy... 8-) Assuming you have a board class that has the 8x8 pieces, then you can do something like this: Board a; ... Board b(a); The array should be hidden inside the board and all accesses done with a Piece get(int x, int y);. – Alexis Wilke Jan 02 '12 at 05:56
0

You can copy by allocating memory at the destination and then memcopy

dest_pointer = (<<my type>>*) malloc(sizeof(<<my type>>);
memcpy(dest_pointer, src_pointer, sizeof(<<my type>>);

Btw, the methods are never copied. they don't belong to an object.

Aater Suleman
  • 2,286
  • 18
  • 11