C Segmentation Fault Help

This is a discussion on C Segmentation Fault Help within the C Programming forums, part of the General Programming Boards category; Hi, I'm having some problems getting the following code to run without reaching a segmentation fault error. It compiles and ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    2

    C Segmentation Fault Help

    Hi, I'm having some problems getting the following code to run without reaching a segmentation fault error. It compiles and gives the proper output, but does not close properly and I can't for the life of me figure out why. You can see the code at this link (C code - 50 lines - codepad) or below:

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    /*
    Function: count_match_char_in_array
    Purpose:
            Take input character array and count number of characters that match
            an input character value without regard to case and return the count found
    
    InputArray - Array to scan for matching characters
    MatchChar - Character to match against
    ReturnCount - Count of how many characters passed in MatchChar are found in InputArray
    
    Returns:
            Number of characters in InputArray
    
    */
    int count_match_char_in_array(const char *InputArray, char MatchChar, int *ReturnCount)
    {
            int i;
    	*ReturnCount = 0;
    
            for(i=0;InputArray[i] != '\0';i++)
            {
                    /* If we match the input char, bump the character count */
                    if(tolower(InputArray[i]) == MatchChar)
                    {
                            (*ReturnCount)++;
                    }
    
            }
            return(i);
    }
    
    /*
    Purpose:
            Test CountMatchCharInArray function
            Will input test string and print out the matching count and the number
                    of characters in the input test string
    */
    main()
    {
            int *Count;
            int CharCount;
    
            CharCount = count_match_char_in_array("Do you know the way to San Jose?", 'a',
                            Count);
    
            printf("Found %d occurrences in a total length of: %d\n", *Count, CharCount);
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You had a null pointer assignment. Correct version:

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    /*
    Function: count_match_char_in_array
    Purpose:
            Take input character array and count number of characters that match
            an input character value without regard to case and return the count found
    
    InputArray - Array to scan for matching characters
    MatchChar - Character to match against
    ReturnCount - Count of how many characters passed in MatchChar are found in InputArray
    
    Returns:
            Number of characters in InputArray
    
    */
    int count_match_char_in_array(const char *InputArray, char MatchChar, int *ReturnCount)
    {
            int i;
    	//*ReturnCount = 0;
    
            for(i=0;InputArray[i] != '\0';i++)
            {
                    /* If we match the input char, bump the character count */
                    if(tolower(InputArray[i]) == MatchChar)
                    {
                            (*ReturnCount)++;
                    }
    
            }
            return(i);
    }
    
    /*
    Purpose:
            Test CountMatchCharInArray function
            Will input test string and print out the matching count and the number
                    of characters in the input test string
    */
    int main()
    {
            int Count=0;
            int CharCount=0;
            printf("\n\n");
            CharCount = count_match_char_in_array("Do you know the way to San Jose?", 'a',
                            &Count);
    
            printf("Found %d occurrences in a total length of: %d\n", Count, CharCount);
    
      (void) getchar();
      return 0;
    }

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    2
    Thank you very much!

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    Quote Originally Posted by Adak View Post
    You had a null pointer assignment.
    You are definitely on the right track, but I slightly disagree with your assessment. The problem in my opinion is not in his counting function (*ReturnCount = 0; is fine), but rather in main with (int* Count We don't want to declare count as a pointer; we want to declare it as an int and then pass it by reference to the counting function, which I noticed is exactly what you did in your correction, but you didn't point out that it was a crucial correction.

    I went ahead and valgrinded the original:
    Code:
    ==2109== Use of uninitialised value of size 8
    ==2109==    at 0x40055D: count_match_char_in_array (foo.c:21)
    ==2109==    by 0x4005CF: main (foo.c:46)
    ==2109== 
    ==2109== Use of uninitialised value of size 8
    ==2109==    at 0x40058E: count_match_char_in_array (foo.c:28)
    ==2109==    by 0x4005CF: main (foo.c:46)
    ==2109== 
    ==2109== Use of uninitialised value of size 8
    ==2109==    at 0x400597: count_match_char_in_array (foo.c:28)
    ==2109==    by 0x4005CF: main (foo.c:46)
    ==2109== 
    ==2109== Use of uninitialised value of size 8
    ==2109==    at 0x4005D7: main (foo.c:49)
    ==2109==
    We have problems here:

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    /*
    Function: count_match_char_in_array
    Purpose:
            Take input character array and count number of characters that match
            an input character value without regard to case and return the count found
    
    InputArray - Array to scan for matching characters
    MatchChar - Character to match against
    ReturnCount - Count of how many characters passed in MatchChar are found in InputArray
    
    Returns:
            Number of characters in InputArray
    
    */
    int count_match_char_in_array(const char *InputArray, char MatchChar, int *ReturnCount)
    {
            int i;
            // [ReturnCount] Use of uninitialised value from Count in main.
    	*ReturnCount = 0;
    
            for(i=0;InputArray[i] != '\0';i++)
            {
                    /* If we match the input char, bump the character count */
                    if(tolower(InputArray[i]) == MatchChar)
                    {
                            // [ReturnCount] Deference and assignment of uninitialised value from Count in main.
                            (*ReturnCount)++;
                    }
    
            }
            return(i);
    }
    
    /*
    Purpose:
            Test CountMatchCharInArray function
            Will input test string and print out the matching count and the number
                    of characters in the input test string
    */
    main()
    {
            int *Count;
            int CharCount;
    
            // [Count] Use of uninitialised value of size 8.
            CharCount = count_match_char_in_array("Do you know the way to San Jose?", 'a',
                            Count);
    
            // [Count] Use of uninitialised value of size 8. CharCount is initialized by count_match_char_in_array() call
            printf("Found %d occurrences in a total length of: %d\n", *Count, CharCount);
    }
    Conceptually the problem is that declaring Count as pointer to int in main means that we don't actually hold information about the count. Only an int holds the actual count. If we want the function count_match_char_in_array() to return it as an out, we have to pass count by reference. That way, our function gets to alter the contents of Count because we have it's address, which is I think what we intended.
    Last edited by QuadraticFighte; 10-16-2010 at 04:31 PM.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That assessment was from my compiler, just before the program crashed. I trust it.

    I never said that *ReturnCount was the problem with the program. If you notice, I left it in there. The assigning of *ReturnCount to zero was REM'd out because it was simply redundant.

    When you have a long line of code or text, inside code tags, like this one:
    printf("Found %d occurrences in a total length of: %d\n", *Count, CharCount); // [Count] Use of uninitialised value of size 8. CharCount is initialized by count_match_char_in_array() call
    Please break it up into two or three lines. Otherwise it "breaks" the forums table width settings.

    Thanks, and

    << Welcome to the Forum, QuadraticFighte!>>

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    Quote Originally Posted by Adak View Post
    When you have a long line of code or text, inside code tags, like this one:

    Please break it up into two or three lines. Otherwise it "breaks" the forums table width settings.
    Sorry, I forget not everyone has their table settings arbitrarily large. And thanks for the welcome :P I hope that's a little better. I also am pleasantly surprised we can format code inside our code tags.
    Last edited by QuadraticFighte; 10-16-2010 at 04:31 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 04-20-2010, 11:55 PM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 06:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 01:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21