Thread: Handling of callback function that is not registered

  1. #1
    Registered User
    Join Date
    Jun 2012
    Location
    Göteborg
    Posts
    28

    Handling of callback function that is not registered

    I have a problem of how to handle registering of a callback function.

    In Example 1, I commented away reg_cb(f1); and in the function irq_handler() the check is working ok (callback = NULL).

    However in the second example, Example 2 where the callback function is an element in a struct, the handling is a bit strange. If I comment away bp->func = f1; it looks like the check does not realize the element is NULL.

    I don't realise what is going on. Can anyone explain this please?

    Regards

    Example 1
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    void f1()
    {
            printf("%s() -\n", __func__);
    }
    
    void (*callback)(void);
    void reg_cb(void(*cb)(void))
    {
            printf("%s() -\n", __func__);
            callback = cb;
    }
     
    void irq_handler(void)
    {
        printf("%s() -\n", __func__);
        if (callback != NULL)
        {
                callback();
        }
        else
        {
                printf("cb not registered\n");
        }
    }
     
    int main(int argc, char *argv[])
    {
        reg_cb(f1);
        irq_handler();
    
        return 0;
    }

    Example 2
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    void f1()
    {
            printf("%s() -\n", __func__);
    }
     
    struct block
    {
        int var;
        void (*func)(void);
    };
    
    
    int main(int argc, char *argv[])
    {
        struct block *bp;
        struct block b;
    
        bp = &b;
        bp->var = 10;
        bp->func = f1;
    
        printf("var=%d\n", bp->var);
    
        if (bp->func) 
        {
               bp->func();
        }
        else
        {
               printf("Not registered\n");
        }
    
        return 0;
    }

  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
    > I don't realise what is going on. Can anyone explain this please?
    > If I comment away bp->func = f1; it looks like the check does not realize the element is NULL.
    Presumably, the lack of assignment leave bp->func as a garbage value, thus defeating the purpose of checking.
    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
    Dec 2017
    Posts
    1,631
    In the first case "callback" is a global variable which means that it is automatically initialized to all-bits-zero, which is a common representation for NULL. However, NULL can be different from all-bits-zero so even in the first program you should explicitly initialize it to NULL in main.


    In the second program, you've switched from using a global variable to using a local variable. Local variables start out with an arbitrary value; they are not automatically initialized to all-bits-zero.

    You should explicitly initialize your variables in both cases.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  4. #4
    Registered User
    Join Date
    Jun 2012
    Location
    Göteborg
    Posts
    28
    OK, I will initialize my variables. Thank you for your help.

    Just did it, and now it's working
    Last edited by hzcodec; 03-24-2018 at 09:34 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. callback function in c
    By sun2493 in forum C Programming
    Replies: 4
    Last Post: 05-18-2015, 03:35 PM
  2. Replies: 2
    Last Post: 11-14-2011, 08:08 AM
  3. callback function
    By koszta in forum C Programming
    Replies: 2
    Last Post: 02-23-2009, 12:51 PM
  4. Callback function???
    By ripper079 in forum C++ Programming
    Replies: 3
    Last Post: 11-14-2002, 09:29 PM
  5. Pointer to member function as callback function
    By ninebit in forum C++ Programming
    Replies: 10
    Last Post: 03-01-2002, 05:52 AM

Tags for this Thread