Segmentation Fault C Programming

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define UPBDECK 52
#define LWBDECK 1
#define FALSE 0
#define TRUE 1

typedef struct {int pipval; char *suit;} CARD;

CARD pack[UPBDECK +1], outpack[UPBDECK + 1];
int played [UPBDECK +1]; /* boolean array of 1 and 0 to show if card played */
int cardcount = 0, gamecount = 1, gameswon = 0;

char *stars    = "****";
char *hearts   = "HRTS";
char *clubs    = "CLBS";
char *diamonds = "DMDS";
char *spades   = "SPDS";
char *cardval  = "*A23456789TJQK";

void shuffle (CARD *deck)
{	
  CARD temp; int i,j;

  for (i = LWBDECK; i <= UPBDECK; i++)
    { 
      j = i + random() % (UPBDECK -i +1);
      temp = deck[i]; 
      deck[i] = deck [j]; 
      deck[j] = temp; 
    } 
}

void initialise (CARD *deck)	/* initialise the pack and the "played" booleans*/
{
  int i, j, k = 1; char *suit;

  for (j = 1; j <= 4; j++)
    {
	switch (j) 
	  { 
	    case 1: suit = hearts; break;
	    case 2: suit = clubs; break;
	    case 3: suit = diamonds; break;
	    case 4: suit = spades; break;
	  }

	for (i = 1; i<= 13; i++)
	   { 
	     deck[k].pipval = i; 
	     deck[k].suit = suit;
	     outpack[k].pipval =0;
	     outpack[k].suit = stars; 
		k++; /* k is incremented within a 1 -> 13 loop inside an outer 1 -> 4 loop Hence it cannot exceed 52 */
	   }
     }		
/*initialise the booleans */
  for (i = LWBDECK -1; i <= UPBDECK; i++) 
    played[i] = FALSE;
}

void printgame(CARD *outdeck)
{
  int i=1, j=1;

  printf("OUTCOME OF THE GAME WAS AS FOLLOWS\n\n");
  while(i<13)
    {
      printf("    %c %s    %c %s    %c %s\n",
	     cardval[outdeck[j+10].pipval],outdeck[j+10].suit, 
	     cardval[outdeck[j+11].pipval],outdeck[j+11].suit, 
	     cardval[outdeck[j].pipval],outdeck[j].suit);
      i += 3;
      j +=13;
    }
  printf("\n\n\n");
  j=1;
  while(i>=13 && i<=20)
    {
      printf("    %c %s              %c %s\n",
	     cardval[outdeck[j+9].pipval],outdeck[j+9].suit, 
	     cardval[outdeck[j+1].pipval],outdeck[j+1].suit);
      i += 2;
      j +=13;
    }
  printf("\n\n\n");
  j=1;
  while(i>=21 && i<=32 )
    {
      printf("    %c %s    %c %s    %c %s\n",
	     cardval[outdeck[j+8].pipval],outdeck[j+8].suit, 
	     cardval[outdeck[j+12].pipval],outdeck[j+12].suit, 
	     cardval[outdeck[j+2].pipval],outdeck[j+2].suit);
      i += 3;
      j += 13;
    }
  printf("\n\n\n");
  j=1;
  while(i>=33 && i<=40)
    {
      printf("    %c %s              %c %s\n",
	     cardval[outdeck[j+7].pipval],outdeck[j+7].suit, 
	     cardval[outdeck[j+3].pipval],outdeck[j+3].suit);
      i += 2;
      j += 13;
    }
  printf("\n\n\n");
  j=1;
  while(i>=41 && i<=52 )
    {
      printf("    %c %s    %c %s    %c %s\n",
	     cardval[outdeck[j+6].pipval],outdeck[j+6].suit, 
	     cardval[outdeck[j+5].pipval],outdeck[j+5].suit, 
	     cardval[outdeck[j+4].pipval],outdeck[j+4].suit);
      i += 3;
      j += 13;
    }
  printf("\n\ngamecount = %d  cardcount= %d\n", gamecount, cardcount);

}

void setoutpack(int pos)
{
  int new;

  int again = TRUE;
  new = pack[pos].pipval;

  do
    {
      if(played[new]== 0)
	{
	  outpack[new]=pack[pos];
	  played[new]=TRUE;
	  again = FALSE;
	}
      else
	{
	  new += 13;
	  again = TRUE;
	}
    }while(again==1);
  
}
int main (char argc, char *argv[])
{
  int i=1,j=0, kingcount=0, pointer = UPBDECK;

  srandom((unsigned int) time(NULL));    

  initialise(pack);
  shuffle(pack);
   do
    {  
      setoutpack(i);
      cardcount++;
      j= pack[i].pipval;
      i=j +13;

      if(j==13)
	kingcount++;
      
   
      }while(kingcount <4);
  printgame(outpack);
  if(cardcount==52 && kingcount==4)
      printf("Game completed!! Chance of this was 1 in 13 \n\n");
}/*end of main */


i believe the problem lies on the addition of 13 and j into i.(the commented) But i don't understand what's the fault... Anyone keen to help?
Last edited on
Are you sure you're not overstepping the bounds of your array?

EDIT: Your segmentation fault is on line 135. I hope that helps.

-Albatross
Last edited on
the fault only appear when i did not comment line 160 out and increment i instead.
like this
1
2
3
4
5
6
7
8
9
10
11
12
do
    {  
      setoutpack(i);
      cardcount++;
      j= pack[i].pipval;
      i++;

      if(j==13)
	kingcount++;
      
   
      }while(kingcount <4);


so i dun think line 135 has any problem
Last edited on
Well, when I ran your program through gdb, it complained about line 135, not line 160. :/

-Albatross
Last edited on
o.o any idea about the fault on line 135?
Out of bounds. You keep on doing new += 13;
the fault only appear when i did not comment line 160 out and increment i instead.
IIRC in that case setoutpack() will not loop, as the first guess will be empty.

As 'out of bounds' is undefined behaviour, ¿do you know how to configure gcc (or gdb) in order to yell in that case?
Topic archived. No new replies allowed.