Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Writing a callback function?

Writing a callback function?

Scheduled Pinned Locked Moved C / C++ / MFC
jsonquestion
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Maxwell Chen
    wrote on last edited by
    #1

    I am trying to write a simple callback function for my own purpose, not to pass into Windows API as callback arguments. What I did below gets access violation. Where did I do wrong in the below code? Thanks.

    typedef void (*fC)(int* p);

    bool Test(int n, fC* p)
    {
    int* pN = new int;
    *pN = 3;
    (*p)(pN); // ---- access violation 0xc0000005.
    delete pN;

    return true;
    

    }

    void fc(int* p)
    {
    *p;
    }

    void main()
    {
    Test(3, (fC*)&fc);
    }

    Maxwell Chen

    E C I 3 Replies Last reply
    0
    • M Maxwell Chen

      I am trying to write a simple callback function for my own purpose, not to pass into Windows API as callback arguments. What I did below gets access violation. Where did I do wrong in the below code? Thanks.

      typedef void (*fC)(int* p);

      bool Test(int n, fC* p)
      {
      int* pN = new int;
      *pN = 3;
      (*p)(pN); // ---- access violation 0xc0000005.
      delete pN;

      return true;
      

      }

      void fc(int* p)
      {
      *p;
      }

      void main()
      {
      Test(3, (fC*)&fc);
      }

      Maxwell Chen

      E Offline
      E Offline
      Eytukan
      wrote on last edited by
      #2

      change

      bool Test(int n, fC* p)

      to

      bool Test(int n, fC p)

      Also remove the cast.

      void main()
      {
      Test(3, (fC*) &fc);
      }

      He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus

      M 1 Reply Last reply
      0
      • M Maxwell Chen

        I am trying to write a simple callback function for my own purpose, not to pass into Windows API as callback arguments. What I did below gets access violation. Where did I do wrong in the below code? Thanks.

        typedef void (*fC)(int* p);

        bool Test(int n, fC* p)
        {
        int* pN = new int;
        *pN = 3;
        (*p)(pN); // ---- access violation 0xc0000005.
        delete pN;

        return true;
        

        }

        void fc(int* p)
        {
        *p;
        }

        void main()
        {
        Test(3, (fC*)&fc);
        }

        Maxwell Chen

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #3

        Maxwell Chen wrote:

        bool Test(int n, fC* p)

        Change to

        bool Test(int n, fC p)

        Maxwell Chen wrote:

        Test(3, (fC*)&fc);

        Change to

        Test(3, fc);

        :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        M 1 Reply Last reply
        0
        • C CPallini

          Maxwell Chen wrote:

          bool Test(int n, fC* p)

          Change to

          bool Test(int n, fC p)

          Maxwell Chen wrote:

          Test(3, (fC*)&fc);

          Change to

          Test(3, fc);

          :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          M Offline
          M Offline
          Maxwell Chen
          wrote on last edited by
          #4

          Thanks! ;P

          Maxwell Chen

          1 Reply Last reply
          0
          • E Eytukan

            change

            bool Test(int n, fC* p)

            to

            bool Test(int n, fC p)

            Also remove the cast.

            void main()
            {
            Test(3, (fC*) &fc);
            }

            He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus

            M Offline
            M Offline
            Maxwell Chen
            wrote on last edited by
            #5

            Ooops! My bad ... :laugh:

            Maxwell Chen

            E 1 Reply Last reply
            0
            • M Maxwell Chen

              I am trying to write a simple callback function for my own purpose, not to pass into Windows API as callback arguments. What I did below gets access violation. Where did I do wrong in the below code? Thanks.

              typedef void (*fC)(int* p);

              bool Test(int n, fC* p)
              {
              int* pN = new int;
              *pN = 3;
              (*p)(pN); // ---- access violation 0xc0000005.
              delete pN;

              return true;
              

              }

              void fc(int* p)
              {
              *p;
              }

              void main()
              {
              Test(3, (fC*)&fc);
              }

              Maxwell Chen

              I Offline
              I Offline
              Iain Clarke Warrior Programmer
              wrote on last edited by
              #6

              I've been looking at callback stuff of my own, and if you your code to:

              typedef void (*fC)(int* p);

              bool Test(int n, fC p)
              {
              int* pN = new int;
              *pN = 3;
              (p)(pN); // ---- access violation 0xc0000005.
              delete pN;

              return true;
              

              }

              void fc(int* p)
              {
              *p;
              }

              void main()
              {
              Test(3, (fC)fc);
              }

              Then it works just fine... But your code doesn't look bad to me. Just more indirection than you need. fC is already a type of "pointer to a function", so why make things work with "pointer to a pointer to a function"? In the interests of science, I added these two lines to main:

              fC p0 = fc;
              fC \*p1 = &fc;
              

              And on the second line I get: "error C2440: 'initializing' : cannot convert from 'void (__cdecl *)(int *)' to 'void (__cdecl ** )(int *)'". This leads me to suggest that you're seeing the same behaviour as when you try to get a pointer to an array - you get the first element of the array back. This is probably a C thing that's biting you. And then you manually cast it in your original main and things blow up later... That's my theory, anyway! Iain.

              In the process of moving to Sweden for love (awwww). If you're in Scandinavia and want an MVP on the payroll (or happy with a remote worker), give me a job!

              M C 2 Replies Last reply
              0
              • M Maxwell Chen

                Ooops! My bad ... :laugh:

                Maxwell Chen

                E Offline
                E Offline
                Eytukan
                wrote on last edited by
                #7

                :| Laugh is not enough.. You owe me something? :rolleyes: :-D

                He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus

                1 Reply Last reply
                0
                • I Iain Clarke Warrior Programmer

                  I've been looking at callback stuff of my own, and if you your code to:

                  typedef void (*fC)(int* p);

                  bool Test(int n, fC p)
                  {
                  int* pN = new int;
                  *pN = 3;
                  (p)(pN); // ---- access violation 0xc0000005.
                  delete pN;

                  return true;
                  

                  }

                  void fc(int* p)
                  {
                  *p;
                  }

                  void main()
                  {
                  Test(3, (fC)fc);
                  }

                  Then it works just fine... But your code doesn't look bad to me. Just more indirection than you need. fC is already a type of "pointer to a function", so why make things work with "pointer to a pointer to a function"? In the interests of science, I added these two lines to main:

                  fC p0 = fc;
                  fC \*p1 = &fc;
                  

                  And on the second line I get: "error C2440: 'initializing' : cannot convert from 'void (__cdecl *)(int *)' to 'void (__cdecl ** )(int *)'". This leads me to suggest that you're seeing the same behaviour as when you try to get a pointer to an array - you get the first element of the array back. This is probably a C thing that's biting you. And then you manually cast it in your original main and things blow up later... That's my theory, anyway! Iain.

                  In the process of moving to Sweden for love (awwww). If you're in Scandinavia and want an MVP on the payroll (or happy with a remote worker), give me a job!

                  M Offline
                  M Offline
                  Maxwell Chen
                  wrote on last edited by
                  #8

                  Thanks! :-D

                  Maxwell Chen

                  1 Reply Last reply
                  0
                  • I Iain Clarke Warrior Programmer

                    I've been looking at callback stuff of my own, and if you your code to:

                    typedef void (*fC)(int* p);

                    bool Test(int n, fC p)
                    {
                    int* pN = new int;
                    *pN = 3;
                    (p)(pN); // ---- access violation 0xc0000005.
                    delete pN;

                    return true;
                    

                    }

                    void fc(int* p)
                    {
                    *p;
                    }

                    void main()
                    {
                    Test(3, (fC)fc);
                    }

                    Then it works just fine... But your code doesn't look bad to me. Just more indirection than you need. fC is already a type of "pointer to a function", so why make things work with "pointer to a pointer to a function"? In the interests of science, I added these two lines to main:

                    fC p0 = fc;
                    fC \*p1 = &fc;
                    

                    And on the second line I get: "error C2440: 'initializing' : cannot convert from 'void (__cdecl *)(int *)' to 'void (__cdecl ** )(int *)'". This leads me to suggest that you're seeing the same behaviour as when you try to get a pointer to an array - you get the first element of the array back. This is probably a C thing that's biting you. And then you manually cast it in your original main and things blow up later... That's my theory, anyway! Iain.

                    In the process of moving to Sweden for love (awwww). If you're in Scandinavia and want an MVP on the payroll (or happy with a remote worker), give me a job!

                    C Offline
                    C Offline
                    CPallini
                    wrote on last edited by
                    #9

                    Iain Clarke wrote:

                    In the interests of science

                    :-D

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups