#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Bournemouth(U.K.)
    Posts
    24
    Rep Power
    0

    Segmentation Fault!


    Code:
    char cDeckofCards[NUM_CARDS][MAX_LENGTH]=
    
    		{"AC", "2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "10C", "JK", "QC", "KC",
    		 "AD", "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "10D", "JD", "QD", "KD",
    		 "AH", "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "10H", "JH", "QH", "KH",
    		 "AS", "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "10S", "JS", "QS", "KS"
    		};
    
    		int iCardPoints[NUM_CARDS]=
    		{
    			1,2,3,4,5,6,7,8,9,10,10,10,10,
    			1,2,3,4,5,6,7,8,9,10,10,10,10,
    			1,2,3,4,5,6,7,8,9,10,10,10,10,
    			1,2,3,4,5,6,7,8,9,10,10,10,10
    		};
    
    		int iCardIndex;					/*To control which card is being manipulated*/
    		int iRandomNumber;				/*Variable to store random number when shuffling*/
    		char cTempCard[MAX_LENGTH];		/*Temporary storage of card being shuffled*/
    
    
    		for(iCardIndex=0; iCardIndex < NUM_CARDS; iCardIndex++)
    		{
    				iRandomNumber= abs(rand()%52);
    
    				strcpy(&cTempCard[MAX_LENGTH], &cDeckofCards[iCardIndex]);
    				strcpy(&cDeckofCards[iCardIndex], &cDeckofCards[iRandomNumber]);
    				strcpy(&cDeckofCards[iRandomNumber], &cTempCard[MAX_LENGTH]);
    
    
    
    		}
    Please can someone explain to me why I get a segmentation fault when I run the program. Also, can someone help with the message from the compiler:-

    warning: passing arg 2 of 'strcpy' from incompatible pointer type

    Thank you for your help!

    p.s. the program is written in C!
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    The short explanation is that a segmentation fault occurs when you try to access memory outside of the memory space allotted you by the operating system. In C, this most often happens when a pointer is used improperly -- most often by not having been initialized but also by using a non-pointer as a pointer.

    First, what is the value of MAX_LENGTH? It should be at least 3. In C, strings must be terminated by a NULL ('\0') and so are refered to as being "null-terminated". This means that each and every time you allocate space for a string, you must allocate one more byte beyond the length of the string; e.g., for "Hello" you must allocate six characters, five for "Hello" and one more for '\0'.

    If the null-terminator is left off of a string, then the processing of that string just keeps going on and on until a zero byte is finally found. If you are reading the string, that usually only causes you to get a lot of garbage in your output. If you are writing the string, then you will overwrite other data, including return addresses, which will cause the program to crash.

    If it turns out that MAX_LENGTH is 3, then the first strcpy, strcpy(&cTempCard[MAX_LENGTH], &cDeckofCards[iCardIndex]);, is the culprit. You are starting to write to cTempCard right on top of its null-terminator, so you are overwriting whatever follows it. BTW, this is also refered to as "clobbering" that data, the results of which are usually fatal for the program, though this is basically the "buffer overflow" technique used by hackers.

    I can't figure out what you are trying to do with your code.

    cDeckofCards are what you are reading from and so should not be written to -- exception being if you are going to shuffle that deck, but then you should still not be writing to it when you deal the cards. I'm guessing that you are shuffling the deck here. OK, now I see what you're trying to do.

    cTempCard's length tells me that it contains a single card. Since I now realize that this code is shuffling the deck, I see its size is correct, you're just not using it correctly. You need to reference the beginning of the array, not the end.

    strcpy copies from one char array to another. The number of characters copied depends on when it finds the null-terminator of the source array. It takes as its parameters two char pointers; in C, an array name is the same thing as a pointer.

    So normally, you would just pass to strcpy the names of the source and destination arrays. However, if one of them starts after the first char of the array (eg, you want to skip the first char of the source string), then the notation of &array[index] is correct since that would be a char pointer. But that is not what you need to do here, even though that is the way you wrote it.

    You are getting the warnings mainly because of how you are trying to reference an individual string in the deck array. When you get a warning like that, the compiler is telling you to take a closer look at what you are passing to that function. cDeckofCards[iCardIndex] references a char array, so its datatype is already a char pointer (char*). By taking its address with &cDeckofCards[iCardIndex], you are passing to strcpy a pointer to a char pointer (char**), which is a different level of indirection than it expects. Actually, that would also cause large "string" to be copied to a much smaller string, which would result on a lot of data getting clobbered. Not a good thing.

    Here is what I think you are trying to write:
    Code:
    strcpy(cTempCard, cDeckofCards[iCardIndex]);
    strcpy(cDeckofCards[iCardIndex], cDeckofCards[iRandomNumber]);
    strcpy(cDeckofCards[iRandomNumber], cTempCard);
    Last edited by dwise1_aol; September 22nd, 2003 at 11:48 AM.

IMN logo majestic logo threadwatch logo seochat tools logo