Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Array/Pointer is causing a STATUS_ACCESS_VIOLATION is g++

Reply
Thread Tools

Array/Pointer is causing a STATUS_ACCESS_VIOLATION is g++

 
 
SB
Guest
Posts: n/a
 
      10-30-2003
Hello! I have two classes, Player and CardDeck. CardDeck contains an int
array called drawCards(int drawCards[12]. Among other things, the Player
class contains a static CardDeck object(static CardDeck c. These are both
in a header file. From my other .cpp file with main, I have an array of
players (Player players[11]. My goal is to give access to a single deck of
cards (drawCards) that was initialized in the CardDeck class to the Player
class - obviously, it has to be static because all 11 players need to share
the same deck of cards - not each have their own deck. I was able to
accomplish this and have verified the values in the drawCard array in the
Player class are matching the values of the drawCards deck in the CardDeck
class.
Now for the problem...everything worked fine until I'm assigning each player
from the player array a card (an int value) out of the drawCards array.
Everytime it will assign 9 players a card, then I get this message:
(actually, I'll paste the output to show that the first 9 are getting a
card)
a draws a 5 of Spades
b draws a Jack of Spades
c draws a 7 of Spades
d draws a 6 of Spades
e draws a 2 of Spades
f draws a Queen of Spades
g draws a 8 of Spades
h draws a King of Spades
i draws a 9 of Spades
[main] C:\Program Files\Microsoft Visual Studio\MyProjects\Holdem\a.exe 1000
(0)
handle_exceptions: Exception: STATUS_ACCESS_VIOLATION
[main] a 1000 (0) handle_exceptions: Dumping stack trace to a.exe.core

Here is the code that I think anyone will need to look at to help me solve
this, and I'll add a comment on the line that I believe is causing the
error:
From the CardDeck class:
int drawCards[12];

From the Player class:
int cardsInHand[2];

// gets the draw cards
int* CardDeck::getDrawCards()
{
// show the cards
test(drawCards);

return drawCards;
}

void CardDeck::test(int a[])
{
for (int i = 0; i < 12; i++)
cout<<"test card is "<<a[i]<<endl;
}

void Player::drawCards(Player players[], CardDeck c)
{
int drawCardRank = 0;
int drawCardSuit = 0;

// get the cards
int* cards = c.getDrawCards(); //*NOTE* getDrawCards returns a pointe, but
i return it drawCards array. is this correct? wouldn't compile otherwise
cout<<endl;
//c.test(cards); //*NOTE* if i uncomment this, all 12 cards will display
without error, so i know the array values are not the problem
for (int i = 0; i < numberOfPlayers; i++)
{
// each player draws one card
players[i].cardsInHand[i] = cards[i]; //*NOTE* I believe this is the
problematic line
// map the card
drawCardSuit = players[i].cardsInHand[i] / 100; // get the actual suit for
each card
drawCardRank = players[i].cardsInHand[i] % 100; // get the actual rank for
each card
*NOTE* mapCards simply returns a string for the rank and suit so the
user will see an actual card instead of an int value
cout<<players[i].name<<" draws a "<<c.mapCards(drawCardRank,
drawCardSuit)<<endl;
}
}

Here is main, in its entirety:
#include "Holdem.cpp"

using namespace std;

int main()
{
srand(time(0));
CardDeck cards;
// this needs to be a 11 element array - 10 players, one dealer
Player players[11];
players[0].initializePlayerArray(players); // all this does is gives access
to the array of players from main to the functions in the player class
players[0].drawCards(players, cards);
return 0;
}

Can anyone help me figure this out? My assumption is that I'm just not
assigning the first element of the cardsInHand array of each player a value
from the drawCards array properly, but I don't know how else to do it.

Thanks in advance!!!


 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      10-30-2003

"SB" <(E-Mail Removed)> wrote in message news:a28ob.122478$a16.6145@lakeread01...
\
> for (int i = 0; i < numberOfPlayers; i++)
> {
> // each player draws one card
> players[i].cardsInHand[i] = cards[i]; //*NOTE* I believe this is the
> problematic line


Of course it's the problematic line i numberOfPlayers is greater than 2.
Just what do you think you are doing using the same index everywhere.
cardsInHand is only two elements.

It would appear you want seperate indexes for all three of those arrays.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
.Net Seems to be Causing DLL Memory Leak? =?Utf-8?B?SWxhbiBPc3Npbg==?= ASP .Net 1 03-09-2005 05:38 PM
Firefox causing REAL player to crash Axl Firefox 0 02-27-2005 10:49 PM
Neighbors have wireless..maybe causing problems for my wirless con =?Utf-8?B?Q3Jld2UgRGF2ZQ==?= Wireless Networking 3 01-03-2005 09:19 PM
WI-FI PCMCIA card causing Vaio GRX laptop to crash a boot =?Utf-8?B?QXJyYW4=?= Wireless Networking 2 10-23-2004 10:52 PM
Unknown Trojan causing wireless connection to fail Headtheball Wireless Networking 1 10-03-2004 03:02 PM



Advertisments