Thread: Seg fault issue

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    Seg fault issue

    Hey guys.

    I am creating a small type and search program and it compiles ok. If I enter the correct word it displays the character string fine then terminates. However, if I enter an incorrect word, it freezes and throws a seg-fault. After using the debugger, the error line is the if statement header. Here is the code:

    Code:
    #include <iostream>
    #include <cstring>
    
    // main function - driver //////////////////////////////////////////////////////
    //
    int main ( void )
    {
       int i;
       char *pSearchGrid[][ 2 ] = 
       {
          "pencil", "A writing device",
           "amelle", "One of the Sugababes" ,
           "computer", "Hardware device that uses software" ,
           "phone", "Device that makes and recieves telephone calls" ,
           "picture", "Drawn or computerized image",
           "", "" 
       };
       
       char word[ 80 ];
       
       std::cout << "Enter a word to find: ";
       std::cin >> word;
       
       for ( i = 0; pSearchGrid[ i ][ 0 ]; i++ )
       {
           if ( !strcmp ( pSearchGrid[ i ][ 0 ], word ))
           {
              std::cout << std::endl << pSearchGrid[ i ][ 1 ] << std::endl;
              break;
           }
       }
       
       if ( !*pSearchGrid[ i ][ 0 ] )  // SEG FAULT DEBUG BREAK HERE
       {
          std::cout << word << " not found...\n";
       }
              
       std::cin.get(); // freeze console output window
       std::cin.ignore();
       
       return 0; // return value from int main
    }
    I have looked over it and cannot see why it keeps breaking here. Can you see what I have done wrong? I appreciate any help.
    Double Helix STL

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Apparently the end terminator "" has a non-null address and doesn't stop the loop where you want to. You probably want to check if the first character of that string is '\0'.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    Unless you absolutely need to do it that way, an std::map would be much more appropriate.

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Quote Originally Posted by anon View Post
    Apparently the end terminator "" has a non-null address and doesn't stop the loop where you want to. You probably want to check if the first character of that string is '\0'.
    Thanks!

    I changed the for loop line to

    Code:
    for ( i = 0; *pSearchGrid[ i ][ 0 ]; i++ )
    and its working now

    And Desolation, we havent covered maps yet but when we do I am sure I will understand your meaning further. Thank you for the advice guys.
    Double Helix STL

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
       char *pSearchGrid[][ 2 ] = 
       {
          "pencil", "A writing device",
           "amelle", "One of the Sugababes" ,
           "computer", "Hardware device that uses software" ,
           "phone", "Device that makes and recieves telephone calls" ,
           "picture", "Drawn or computerized image",
           NULL, NULL
       };
    would be a better change, as:
    1. It makes it obvious that it's the end of the list.
    2. Takes up (slightly) less space.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting a seg fault
    By ammochck21 in forum C Programming
    Replies: 11
    Last Post: 01-23-2009, 05:27 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  4. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 08:27 PM
  5. Seg Fault Problem
    By ChazWest in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2002, 03:24 PM