Thread: function pointer issue

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    25

    function pointer issue

    Hello all. I'm working on a project that utilizes some existing code, but I only have the .h and .o files. One function, list_find, is declared as:

    Code:
    void   *list_find(tList, void*, int(*compare)());
    I'm using that function to locate the "matrix cell" that I am looking for.

    Comparison function: (not, prints are so that I know it's being called)
    Code:
    int compareMatrixData(void * one, void * two)
    {
      tMatrixData first = (tMatrixData) one;
      tMatrixData second = (tMatrixData) two;
      printf("%%%%Rows: %i, %i\n", first->row, second->row);
      printf("%%%%Columns: %i, %i\n", first->column, second->column);
      if((first->row == second->row) && (first->column == second->column))
        return 1;
      else
        return 0;
    }
    That function is passed in:
    Code:
    void * matrix_at(tMatrix m, int i, int j)
    {
      tMatrixData temp = malloc(sizeof(struct sMatrixData));
      if(temp == NULL)
      {
        printf("Out of memory, allocation failed, exiting\n");
        exit(-1);
      }
      temp->row = i;
      temp->column = j;
      tMatrixData at = (tMatrixData) list_find(m->data, temp,
                         compareMatrixData);
      free_matrix_data(temp);
      if(at == NULL)
        return 0;
      else
        return at->value;
    }
    I am not seeing any of those wonderful print statements, and I know for a fact that matrix_at is returning 0 every time. I am wondering why compareMatrixData is failing to be called, and without the .c file I cannot really see what exactly is expected.

    Thanks for any input!

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    What compiler are you using?

    Also... turn your compiler's warning level up to maximum... treat each warning as a problem to be fixed.

    If you are using anything but a C-99 standards compliant compiler it's going to puke on this line...

    Code:
      tMatrixData at = (tMatrixData) list_find(m->data, temp,
                         compareMatrixData);
    In anything but c-99 you are required to declare your variables at the top of the function. You can assign to them anywhere.. but they must exist before first use.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Presumably list_find() has some issue with your parameters and so returns NULL. You could always try setting errno to 0 and then checking it after the call, on the chance it's because of the result of some standard function call and a clue might help.

    I'd try writing as short a program as possible using list_find() and keep plugging stuff into it until you get it to work, or run out of ways to try.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Can we see the definition of list_find? That seems to be where the problem is, and it's the one function we don't have.
    EDIT: I'm stupid...can't read very well. Strike the above paragraph.

    Are you sure m->data actually contains a list and that list contains the element you're searching for? Can you print the contents of temp and m->data before searching to make sure?

    As MK27 suggested, a small example that isolates the problem would be ideal.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    25
    I will work on creating a small main that will just print the contents of the list one by one. The issue is that I am working with (as assigned) someone else's list adt, so I don't have access to the function definitions, only the object code and declarations.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    25
    Also I'm using gcc. We are using the latest c standards I believe so the "pukable" lines are "okay."

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    25
    EDIT: I've determined one issue, and that's that the list insertions return the list. I had not been accounting for that. I am now encountering a seg fault and will work on diagnosing that.

    It turns out I'm having similar trouble with printing. Again the only way I have to iterate over the list is to use the provided list_foreach(list, function) method. For some reason the following call:

    Code:
    list_foreach(m->data, free_matrix_data);
    Works just fine; however, this:

    Code:
    void print_list(tMatrix m)
    {
      list_foreach(m->data, print_data);
    
      //printf("Printing list data \n");
    }
    Does not work. The print_data method looks like:
    Code:
    void print_data(tMatrixData md)
    {
      printf("Calling print_data \n");
      printf("Value: %i \n", (int)md->value);
      printf("Row: %i \n", md->row);
      printf("Column: %i \n", md->column);
    }
    I have put prints into the free_matrix_data function as well, and it does get called.

    The entire tList header looks like:
    Code:
    /* list.h */
    
    
    #ifndef _LIST
    #define _LIST
    
    
    typedef struct sList *tList;
    
    
    /* exported list functions */
    
    
    tList   list_copy (tList);
    void   *list_find(tList, void*, int(*compare)());
    void   *list_first(tList);
    void    list_foreach(tList, void (*f)());
    void    list_foreach1(tList, void (*f)(), void*);
    void    list_foreach2(tList, void (*f)(), void*, void*);
    void    list_foreach3(tList, void (*f)(), void*, void*, void*);
    void    list_foreach4(tList, void (*f)(), void*, void*, void*, void*);
    void    list_free(tList);
    tList   list_initialize();
    tList   list_insert_end(tList, void*);
    tList   list_insert_beginning(tList, void*);
    tList   list_insert_in_order(tList, void*, int (*compare)());
    int     list_isempty(tList);
    int     list_length(tList);
    int     list_member(tList, void*, int (*compare)());
    void*   list_remove(tList, void*, int (*compare)());
    void*   list_remove_first(tList);
    #define list_remove_any(l) list_remove_first(l)
    int tracer;
    void    list_foreach_pair(tList, tList, void (*f)());
    void    list_foreach_pair1(tList, tList, void (*f)(), void *);
    void    list_foreach_pair2(tList, tList, void (*f)(), void *, void*);
    void    list_foreach_triple1(tList, tList, tList, void (*f)(), void*);
    void    list_foreach_triple2(tList, tList, tList, void (*f)(), void*, void*);
    
    #endif
    Last edited by drshmoo; 09-22-2011 at 03:03 PM.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    What does free_matrix_data look like? How many times does free_matrix_data get called? Are you sure it's working, and not simply running several times but not actually freeing anything? Some actual output might help, and maybe more of your code, so we can make sure you don't have any errors in building your list.
    Code:
    void    list_foreach(tList, void (*f)());
    Unfortunately, the empty () at the end of that parameter don't tell you how you're supposed to define the callback, i.e. what parameters list_foreach passes to it, so you may be defining print_data and compareMatrixData incorrectly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer issue
    By Justinb in forum C Programming
    Replies: 3
    Last Post: 10-17-2010, 08:26 PM
  2. Help with pointer issue
    By ph5 in forum C Programming
    Replies: 4
    Last Post: 11-17-2009, 04:19 PM
  3. pointer issue
    By san_crazy in forum C Programming
    Replies: 4
    Last Post: 10-16-2008, 06:05 PM
  4. Pointer issue
    By pfs in forum C Programming
    Replies: 2
    Last Post: 09-26-2008, 11:37 AM
  5. Pointer issue when looking through .txt
    By kordric in forum C++ Programming
    Replies: 1
    Last Post: 05-17-2008, 11:26 AM