I am writing a poker program to get better aquainted with beginner stuff and have encountered a segmentation fault.
Debugging says: STATUS_STACK_BUFFER_OVERRUN encountered.
The problem surface when I tried to add the ability to draw new cards in the program.
Here's how I have it laid out:
After getting the number of users (I have hard coded this in to test and it makes no difference)
dynamically create arrays to hold the players hands
// create the arrays to hold the players hands
// first create an array to hold the address to each player
int** player_array_ptr = new int*[ players ];
// now create an array for each players hand
for (int array_player = 0; array_player < players; array_player++){
player_array_ptr[array_player] = new int[cards_to_deal];
}
call the dealer function to fill the hands (works perfectly)
the call
for (int player = 0; player < players; player++){
dealer(player_array_ptr[player]);
}
and the function
int dealer(void* deal_player_array){
// get temporary access to the players hand array
int* deal_access=(int*)deal_player_array;
// declare our variables for this function
int suit, face, n, this_card, new_card;
// set up temporary arrays to hold the hands values
int our_hand_card [5];
int our_hand_suit [5];
// loop until the player has 5 cards
for (int card = 0; card < cards_to_deal; ++card){
// tell the randomizer to decrement
n = randomizer(cards_remaining--);
// get the next available card
this_card = next_available(n);
// modulo and divide to get value for face and suit
face = this_card % 13;
suit = this_card / 13;
// stuff these values into the arrays
our_hand_card[card]=face;
our_hand_suit[card]=suit;
// get a unique int representing suit and face
new_card=((suit*100)+(face));
// put the cards unique int into the players hand
deal_access[card]=new_card;
}
return(0);
}// end function dealer
now the problem area
call the draw function
for (int player = 0; player < players; player++){
draw(player_array_ptr[player]);
}
and the function itself
int draw(void* draw_player_array){
// get temporary access to the players hand array
int* draw_access=(int*)draw_player_array;
// declare our variables for this function
int suit, face, n, this_card, new_card;
// set up temporary arrays to hold the hands values
int our_hand_card [5];
int our_hand_suit [5];
for (int card = 0; card < 5; card++){
if (get_choice(card) == 1){ // start if
// replace a card
// tell the randomizer to decrement
n = randomizer(cards_remaining--);
// get the next available card
this_card = next_available(n);
// modulo and divide to get value for face and suit
face = this_card % 13;
suit = this_card / 13;
// stuff these values into the arrays
our_hand_card[this_card]=face;
our_hand_suit[this_card]=suit;
// get a unique int representing suit and face
new_card=((suit*100)+(face));
// put the cards unique int into the players hand
draw_access[card]=new_card;
}// end if
}// end for
return(0);
}// end function draw
If I never draw a card there is no problem, so the fault lays somewhere inside the if statement in the draw function I think.
I thought maybe i was passing a bad value through the pointer so I hard set the value of new_card to a known valid value, same problem.
Then I tried changing get_choice so it bypassed all the stuff there and always returned a (1) in case my cin stuff was causing problems.
I put in multiple cout's showing what the values of different variables were at crash time and the results were ambiguous. Sometimes it would crash inside the draw function, sometimes back in main. I am sure that a bad value is being passed through the pointer into the array but can't find it (I am totally new to debugging).
I do delete the dynamically created arrays at the end of the program, but I never get there
//delete the dynamic arrays to avoid a memory leak
for (int delete_player = 0; delete_player < players; delete_player++)
delete[] player_array_ptr[ delete_player ];
delete[] player_array_ptr;
What should I try or where should I look?

New Topic/Question
Reply




MultiQuote





|