Here I created a list which is a copy of another list. This list is called copied. I am trying to create a shuffled list of the numbers in copied. The user enters the size of the initial list (how many cards they want), and a list is created and that list is used here to be shuffled. But for some reason when the user enters more that 20, I get this error:
output(41432,0x7fff7389e000) malloc: *** error for object 0x7fff55638490:
pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6
Here is the section of my code. When I remove the while (!copied.empty()) loop, it works fine, as in it puts the "it" value in position "r" from the initial list into the new list. But I need for it to do it for each "r".
list<size_t> copied;
copied.insert(copied.end(), cards.begin(), cards.end());
int r = rand()%(((copied.size()+1) - 0) + 1) + 0;
list<size_t>::const_iterator it = copied.begin();
while(!copied.empty()){
for(int i = 0; i < r; i++) {
it++;}
shuffle.push_back(*it);
copied.erase(it);}