Thread: lfind seg fault

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    2

    lfind seg fault

    lfind is returning a segmentation fault when it cannot find an element in an array on OSX 10.8. I copied the code from Apple (lsearch.c) and found that the comparison (element < end) as part of the for loop in linear_base was returning true even when it should have returned false, so it just ran off the end of the array. Am I passing in wrong arguments? The example below returns:
    Yes (For bsearch with the element present)
    Yes (For lfind with the element present)
    No (For bsearch with the element absent)
    Segmentation fault (For lfind with the element present).
    Thanks in advance for any help.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    static int CompareChar(const void *elemA, const void *elemB) {
      return (*(char *)elemA - *(char *)elemB);
    }
    
    int main() {
      void *alphabet = malloc(26 * sizeof(char));
      int i;
      for(i = 0; i < 26; i++) {
        char ch = 'A' + i;
        memcpy((char*) alphabet + i, &ch, sizeof(char));
      }
    
      char searchCh1 = 'N';
      char searchCh2 = '9';
      int nelem = 26;
    
      bsearch((void*) &searchCh1, alphabet, nelem, sizeof(char), CompareChar) == NULL ? printf("No\n") : printf("Yes\n");
      lfind((void*) &searchCh1, alphabet, &nelem, sizeof(char), CompareChar) == NULL ? printf("No\n") : printf("Yes\n");
    
      bsearch((void*) &searchCh2, alphabet, nelem, sizeof(char), CompareChar) == NULL ? printf("No\n") : printf("Yes\n");
      lfind((void*) &searchCh2, alphabet, &nelem, sizeof(char), CompareChar) == NULL ? printf("No\n") : printf("Yes\n");
     
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You do need to #include <search.h>, but other than that (and the warnings that nelem should be a size_t *, not an int *), I'm not seeing the problem with your code, but I can replicate the segfault on Mac OS X.

    EDIT: It's all about that particular warning. If you declare nelem as a size_t, no more seg fault.
    Last edited by rags_to_riches; 05-12-2012 at 06:42 PM.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    2
    Thank you for the quick response. That fixed it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. lfind() and strcmp()
    By patiobarbecue in forum C Programming
    Replies: 3
    Last Post: 05-04-2010, 10:44 PM
  2. Help with seg fault
    By homer_3 in forum C Programming
    Replies: 3
    Last Post: 02-05-2010, 01:33 PM
  3. seg fault
    By lord in forum C Programming
    Replies: 3
    Last Post: 12-10-2006, 11:15 AM
  4. seg fault
    By chrismiceli in forum C Programming
    Replies: 2
    Last Post: 03-02-2003, 07:33 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM

Tags for this Thread