Question

int newBoard[9][9];
int dirtyBoard[9][9];

/*add stuff to every element of dirtyBoard*/
...

newBoard = &dirtyBoard;

I am trying to make my newBoard be an exact copy of cleanBoard. Since performance is a concern I wanted to see if I could bypass creating a loop to copy each element 1 at a time in favor of changing my pointer address or something. Is this possible?

Was it helpful?

Solution

Use memcpy.

memcpy( newBoard , dirtyBoard , sizeof( newBoard )) ;

Don't forget that both arrays must be of the same size, or at least the array you are copying into must be larger.

assert( sizeof( newBoard ) == sizeof( dirtyBoard ) ) ;

If you want to merely point to dirtyBoard use a pointer.

int (*p)[9] = dirtyBoard ; 

Now p behaves almost exactly as dirtyBoard.

OTHER TIPS

use memcpy

memcpy(newBoard,dirtyBoard, sizeof(newBoard));

Use memcpy:

memcpy(newBoard, dirtyBoard, sizeof(newBoard);

This will copy sizeof(newBoard) bytes starting at dirtyBoard into newBoard. newBoard must be the same size or bigger than dirtyBoard for this to not cause a buffer overflow.

Arrays don't point. You can't "re-point" an array of ints any more than you can "re-point" a single int.

The name of a variable is only ever associated with the storage allocated to that variable; you can't move a variable around in memory, or repurpose the variable's name to a different variable.

However , pointers point. You can make a pointer that points at an array (or a subset of an array), and then change it to point to a different array.

In your case the syntax would be:

int (*newBoard)[9] = &dirtyBoard[0];

This makes newBoard point at a 1-D array of 9 ints . So then you can use the syntax newBoard[i][j] to access the cells.

As you have probably found, you cannot do:

newBoard = &dirtyBoard;

The syntax above however will work like this:

int newBoard[9][9];
int* dirtyBoard;
int i, j;
for(i = 0; i < 9; ++i)
   for(j = 0; j < 9; ++i)
       newBoard[i][j] = i*j;

dirtyBoard = newBoard;

However, then you are not copying, you are effectively making an alias. If you update a value in the pointer you will also update the underlying array and vice versa.

If you want to edit the array via the pointer you can do this sort of thing:

#define cols 9
#define rows 9

...
int n;

int newBoard[rows][cols];
int* dirtyBoard;
int i, j;

int row = 3;
int col = 5;

dirtyBoard[row * cols + col] = 3;  /* to flip from 1 dimensional ptr -> [][] form */

With C++ you don't need to use define's you can use:

const int rows = 9;  // and use as array dimensions.

Since newBoard and dirtyBoard are both statically declared arrays, you can't just point newBoard to the address of dirtyBoard.

If you intend for both to be static arrays, you'll need to copy the data in each element:

for(i=0;i<9;i++)
   for(j=0;j<9;j++)
      newBoard[i][j] = dirtyBoard[i][j];

Edit: As others have mentioned, memcpy() is the way to go, and more efficient.

You could define newBoard as an array of pointer to int, like this:

int (*newBoard)[9];

then you can just do:

newBoard = dirtyBoard;

But, difference here, is that newBoard and dirtyBoard point to the same memory. If you declare two distinct static arrays, you'll end up with two discrete copies of the data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top