0

I have a C++ function that starts as such

void findSolutions(vector<MOVE> & solutions, int board[], int maxPegs){
    int newboard[18];
    copy(begin(board), end(board), begin(newboard));
    ...
    more code
    ...
}

I'm trying to copy the parameter "board" into a temporary int array so I don't directly modify the original board. But in copy(), I get the following error from VSCode

no instance of overloaded function "begin" matches the argument list -- argument types are: (int *)

How do I copy the array that gets passed in from the parameter? Thanks

6
  • you could just use memcpy ? Commented Jan 2, 2020 at 23:20
  • @Spinkoo std::copy likely does compile to a memcpy call for ints Commented Jan 2, 2020 at 23:40
  • 1
    std::copy_n . Commented Jan 2, 2020 at 23:54
  • This is why C++ has classes that encapsulate data and behavior. Write a class that holds the board information. Commented Jan 3, 2020 at 0:21
  • Why are you using std::vector for solutions but not for the board? You can create multidimensional vectors. Commented Jan 3, 2020 at 1:06

2 Answers 2

5

std::copy works in terms of iterators. begin()/end() invoked on a pointer i.e. board will not yield an iterator, instead board being an array which decays to a pointer can be directly used as an iterator. You can simply use std::copy in the following manner.

std::copy(board, board + len, newboard);

I assume len is the length you want to copy and make sure that newboard has the capacity to store len number of elements.

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

2 Comments

Additional helpful reading: What is array decaying?
@user4581301 Thanks for the follow-up. Just edited the answer.
1

You cannot do this safely. An array decays to a pointer when passed into a function. You will have no way to ensure that the source array has enough bytes to read (unless the application is trivial and you know what's going on the caller side). If you pass an array as a pointer to the first element, it's your responsibility to pass the length of the array too. Assuming boardLen the number of ints in board you can do the following,

void findSolutions(vector<MOVE> & solutions, int board[], size_t boardLen, int maxPegs){
    int newboard[18];
    memcpy(newboard, board, min(sizeof(newboard), boardLen*sizeof(int));
    ...

memcpy copies one byte at a time, hence the boardLen*sizeof(int).

8 Comments

This will work if the caller passes the size of the array in bytes which is a little unusual. It's more common to pass the number of elements and for you to do boardLen * sizeof(int) when you call memcpy.
@user4581301 Thanks for the input. But, in aep's answer, how do we make sure that the board has len elements to read ? What happens if board was nullptr ? Where do we get len from ?
@Blastfurnace thanks for the input. I've changed my answer accordingly.
len likely comes from the same place as boardLen; both implementations doomed if the caller lies. If board is NULL, UB results in both aep's answer and this one. A reference to std::array is probably a better choice for board as it all-but-eliminates all of the problems.
I join in your discomfort. There are much better ways to accomplish this. Pretty much any container and the assignment operator makes this all a non-problem.
|

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.