Thread: need assistance with a valgrind error within clang

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    28

    need assistance with a valgrind error within clang

    I amnew to valgrind and I'm trying to figure out what this error means in my program in terms of what valgrind is stating.A little background on the program, I am writing a program that reads in a text file and dictionary file. The text file is compared to the dictionary file, and if a word from the text file does not match a word in the dictionary file, the word is considered misspelled and is displayed within the console. Here is the valgrind output I am getting when I run my program(note that line 141 is the for loop where I traverse through the hash table).
    ==13685== Invalid read of size 4
    ==13685== at 0x8049249: check (dictionary.c:141)
    ==13685== by 0x8048BCF: main (speller.c:115)
    ==13685== Address 0x30 is not stack'd, malloc'd or (recently) free'd
    ==13685==
    ==13685==
    ==13685== Process terminating with default action of signal 11 (SIGSEGV): dumping core
    ==13685== Access not within mapped region at address 0x30
    ==13685== at 0x8049249: check (dictionary.c:141)
    ==13685== by 0x8048BCF: main (speller.c:115)
    ==13685== If you believe this happened as a result of a stack
    ==13685== overflow in your program's main thread (unlikely but
    ==13685== possible), you can try to increase the size of the
    ==13685== main thread stack using the --main-stacksize= flag.
    ==13685== The main thread stack size used in this run was 8388608.
    ==13685==
    And here is the function that is the culprit of this error:
    Code:
     int i=0;
      char DuplicateArray[45];
      int sizevalue=0;
      Node* NodePointer=NULL;
      unsigned int hashval=0;
      int counter=0;
      sizevalue  =sizeof(word);
     
              strncpy(&DuplicateArray[counter], word,sizevalue);
              DuplicateArray[sizevalue-1] = '\0';
              hashval=hash(DuplicateArray);
        
     
              
              
          for (NodePointer=hashtable->table[hashval];NodePointer !=NULL;NodePointer=NodePointer->Next)
          {
          
          i=strcmp(DuplicateArray,NodePointer->word);
          
          if (i==0) 
             {
             
             
             return true;
             }
          else
         
            NodePointer =NodePointer->Next;
          }
       return false;
       }

    Does anyone have any any suggestions on what might be the problem and what valgrind is telling me? Also, if I need to incude more code, please let me know. Thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > ==13685== at 0x8049249: check (dictionary.c:141)
    Yes, you can tell us which line of the code you posted corresponds to line 141 in the original source code you took this snippet from.

    > sizevalue =sizeof(word);
    Do you really mean sizeof or strlen?

    I'm guessing word is a pointer, and it really makes a difference.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    You have several problems but one of the biggest is awful indentation.

    Code:
    sizevalue  =sizeof(word);
    Are you sure you don't want to use strlen()?

    Code:
    for (NodePointer=hashtable->table[hashval];NodePointer !=NULL;NodePointer=NodePointer->Next)
          {
          
          i=strcmp(DuplicateArray,NodePointer->word);
          
          if (i==0) 
             {
             
             
             return true;
             }
          else
         
            NodePointer =NodePointer->Next;
          }
       return false;
       }
    If your comparison isn't equal you advance "NodePointer" twice in each iteration (once in the else-branch, another time in the for-loop) thus you are probably trying to eventually dereference a NULL pointer.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Don't understand why valgrind is giving an error here.
    By dayalsoap in forum C Programming
    Replies: 15
    Last Post: 03-02-2013, 11:59 AM
  2. Clang vs GCC for Linux/Unix Programming?
    By haziz in forum C Programming
    Replies: 2
    Last Post: 05-07-2012, 12:52 PM
  3. Valgrind error help
    By edishuman in forum C Programming
    Replies: 1
    Last Post: 11-12-2011, 03:37 PM
  4. Memory Allocation Error - /w Valgrind Output
    By Ggregagnew in forum C Programming
    Replies: 5
    Last Post: 12-02-2010, 04:02 PM
  5. valgrind error - still reachable
    By myle in forum C Programming
    Replies: 1
    Last Post: 04-19-2009, 08:57 PM