Jus*_*tin 1 c++ arrays out-of-memory segmentation-fault multidimensional-array
到目前为止,我的代码旨在创建一副牌,9 - ACE每个套件的价值,并向4名玩家交易5张牌.这些牌是交易3-2-3-2然后2-3-2-3每个玩家总共有5张牌.稍后我会编写代码来洗牌,然后在euchre游戏中对它们进行比较,但是现在我只想要处理这些卡片.无论如何,它并不完全正常,我愿意接受建议.我得到的错误是"分段错误:11"
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
using namespace std;
int cards[24]; //array filled with card values
char DECK[24][25] =
{ //array filled with card names. Used for dealing //24elements long //25 characters in each element
"Ace of Spades", "Nine of Spades", "Ten of Spades", "Jack of Spades", "Queen of Spades", "King of Spades",
"Ace of Hearts", "Nine of Hearts", "Ten of Hearts", "Jack of Hearts", "Queen of Hearts", "King of Hearts",
"Ace of Clubs", "Nine of Clubs", "Ten of Clubs", "Jack of Clubs", "Queen of Clubs", "King of Clubs",
"Ace of Diamonds", "Nine of Diamonds", "Ten of Diamonds", "Jack of Diamonds", "Queen of Diamonds", "King of Diamonds"
};
int main (void)
{
char P1[5][25];
char P2[5][25];
char P3[5][25];
char P4[5][25];
for( int i = 0 ; i < 24 ; ++i) // for the total amount of cards
{
for(int j = 0; j < 25; j++)
{
//1st deal 3-2-3-2
if(i < 3) // 0 - 2 // 3 cards
{
P1[i][j] = DECK[i][j];
}
if((i > 2) && (i < 5))// 3 - 4 // 2 cards
{
P2[i][j] = DECK[i][j];
}
if((i > 4) && (i < 8)) // 5 - 7 // 3 cards
{
P3[i][j] = DECK[i][j];
}
if((i > 7) && (i < 10))// 8 - 9 // 2 cards
{
P4[i][j] = DECK[i][j];
}
//2nd deal 2-3-2-3
if((i > 9) && (i < 12)) // 10 - 11 // 2 cards
{
P1[i][j] = DECK[i][j];
}
if((i > 11) && (i < 15))// 12 - 14 // 3 cards
{
P2[i][j] = DECK[i][j];
}
if((i > 14) && (i < 17)) // 15 - 16 // 2 cards
{
P3[i][j] = DECK[i][j];
}
if((i > 16) && (i < 20))// 17 - 19 // 3 cards
{
P4[i][j] = DECK[i][j];
}
}
}
for(int q = 0; q < 5; ++q)
{
cout << "P1 has the card " << P1[q]<< endl;
}
for(int q = 0; q < 5; ++q)
{
cout << "P2 has the card " << P2[q]<< endl;
}
//for(int q = 0; q < 5; ++q)
{
//cout << "P3 has the card " << P3[q]<< endl;
}
//for(int q = 0; q < 5; ++q)
{
//cout << "P4 has the card " << P4[q]<< endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
让我"教你钓鱼":
调试器会告诉您故障的确切位置.如果你使用的是IDE(Xcode,Eclipse,VS),它有一个很好的接口,你应该使用它.如果不:
$ gdb my_executable
...
> run
...
Segmentation fault
> where
Run Code Online (Sandbox Code Playgroud)
这将为您提供确切的位置(哪个行号的功能).
| 归档时间: |
|
| 查看次数: |
7339 次 |
| 最近记录: |