473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Array/Pointer is causing a STATUS_ACCESS_VIOLATION is g++

SB
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!!!
Jul 19 '05 #1
1 11064

"SB" <vo******@hotmail.com> 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.
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: masood.iqbal | last post by:
I am having lots of trouble getting a simple program that initializs a dynamically allocated 2D array to work. My 2D array is not getting initialized properly, and additionally I am getting a...
6
by: Adam Hartshorne | last post by:
The input to a function of a 3rd party library I want to use requires a double**, which is a multi-dimension array of doubles. I have looked on the net etc and seen several ways of supposedly...
2
by: Frank | last post by:
Hi, I am developing an application on PPC405 (Walnut). The compiler I am using is GCC. But somehow the string pointer in a C function doesn't work. The code is as below: void example() {...
10
by: Martin Andert | last post by:
Why ist the following code not working? I want to initialize a string in function, but I am getting those STATUS_ACCESS_VIOLATION exceptions. #include <stdio.h> #include <stdlib.h> void...
9
by: Jonathan Burd | last post by:
Greetings everyone, I have noticed a little quirk in C's array-indexing syntax. I wonder what practical purpose this little "inverting" serves to be included in the standard (it compiles fine...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
9
by: AM | last post by:
Hi, I have a C++ Dll that has a function that is being exported as shown below extern "C" __declspec(dllexport) validationResult __stdcall _validateData(double dataToMat, int time); A...
27
by: Jess | last post by:
Hello, the following code failed with "segmentation fault". Could someone tell me why it failed please? Thanks! #include<iostream> #include<string> #include<cstring> #include<cstddef> using...
13
by: stephen b | last post by:
(apologies for cross posting from the moderated group..i'm sure you understand) Hello, I'm passing an array into a Constructor and hoping to use it as a pointer and store it as a class member...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.