1

What is wrong with this statement? It doesn't copy right.

memcpy(new_board1, board, sizeof(board));
3
  • 5
    "Doesn't copy right" in what way? Commented Feb 12, 2011 at 14:12
  • 2
    Can you show the declarations of board and new_board? Commented Feb 12, 2011 at 14:13
  • I think nothing, as long as new_board1 is allocated Commented Feb 12, 2011 at 14:14

2 Answers 2

7

memcpy accepts two memory addresses in form of pointers (destination and source) and the number of bytes to be copied. Now, sizeof doesn't return the size of the memory block a pointer points to, but the size of the pointer itself - either 4 bytes on a 32bit OS, or 8 bytes on a 64bit OS.

Sign up to request clarification or add additional context in comments.

2 Comments

sizeof returns the size, in bytes, of the object passed as its parameter. In case only a type is passed it returns the size of an object of that type. int *ptr = malloc(1000 * sizeof *ptr); int data[1000]; memcpy(ptr, data, sizeof data);
Whether that is 2, 4 or 8 bytes is completely inplementation-dependent. Your OS could be 64-bit while still getting sizeof(void*)=4.
3

sizeof(board) gives you the size of the pointer itself, not the size of whatever it points at. You should replace it by the actual number of bytes you want to copy from board to new_board1. Without knowing how boardand new_board1 are declared, I cannot help you find that number.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.