Search code examples
c++functiontypedef

about function typedef in c++


typedef bool list_less_func (const struct list_elem *a,
                             const struct list_elem *b,
                             void *aux);
void
list_sort (struct list *list, list_less_func *less, void *aux)
{
  size_t output_run_cnt;        /* Number of runs output in current pass. */

  ASSERT (list != NULL);
  ASSERT (less != NULL);

  /* Pass over the list repeatedly, merging adjacent runs of
     nondecreasing elements, until only one run is left. */
  do
    {
      struct list_elem *a0;     /* Start of first run. */
      struct list_elem *a1b0;   /* End of first run, start of second. */
      struct list_elem *b1;     /* End of second run. */

      output_run_cnt = 0;
      for (a0 = list_begin (list); a0 != list_end (list); a0 = b1)
        {
          /* Each iteration produces one output run. */
          output_run_cnt++;

          /* Locate two adjacent runs of nondecreasing elements
             A0...A1B0 and A1B0...B1. */
          a1b0 = find_end_of_run (a0, list_end (list), less, aux);
          if (a1b0 == list_end (list))
            break;
          b1 = find_end_of_run (a1b0, list_end (list), less, aux);

          /* Merge the runs. */
          inplace_merge (a0, a1b0, b1, less, aux);
        }
    }
  while (output_run_cnt > 1);

  ASSERT (is_sorted (list_begin (list), list_end (list), less, aux));
}
void wordcount_sort(word_count_list_t *wclist,
                    bool less(const word_count_t *, const word_count_t *)) {
  list_sort(wclist, less_list, less);
}
static bool less_list(const struct list_elem *ewc1,
                      const struct list_elem *ewc2, void *aux) {
  /* TODO */
    list_less_func* comparefunc;
    if (comparefunc(ewc1, ewc2, aux))
        return false;
    else
        return true;
}

hey guys I think this is a simple c++ question. problem is in less_list(...)function, it should be about function typedef problem. I'm not familiar with this but my deadline is coming. Thanks for help! And you can ignore most of codes in list_sort, important information is just "less" function.


Solution

  • You are calling an uninitialized function pointer. That causes undefined behavior. You have to define a function and assign the function's address to the function pointer:

    struct list_elem {};
    
    typedef bool list_less_func (const list_elem *a,
                                 const list_elem *b,
                                 void *aux);
    
    list_less_func f;
    
    int main() {
        const list_elem *ewc1 = nullptr;
        const list_elem *ewc2 = nullptr;
        void *aux = nullptr;
        list_less_func* comparefunc = f;
        comparefunc(ewc1, ewc2, aux);
    }
    
    bool f (const list_elem *a,
            const list_elem *b,
            void *aux) {
        return a && b && aux;
    }
    

    The function f is just an example. You have to implement a less function.

    Unrelated:

    Instead of

    if (comparefunc(ewc1, ewc2, aux))
        return false;
    else
        return true;
    

    you can write

    return !comparefunc(ewc1, ewc2, aux);