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. How do I convert my C++ function pointer to a C function pointer?

How do I convert my C++ function pointer to a C function pointer?

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++visual-studiodata-structures
6 Posts 5 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.
  • A Offline
    A Offline
    arnold_w
    wrote on last edited by
    #1

    I am writing code for an ARM processor and compiling with GCC inside Eclipse IDE. When the application wants something to get done, it needs to create a task and queue it:

    typedef uint32_t (*task_t)(uint8_t* data, uint16_t dataSizeBytes);
    void queueTask(task_t task, uint8_t* data, uint16_t dataSizeBytes);

    Now I want to be able to queue C++ tasks inside the same task queue:

    #include
    using namespace std;
    using taskCpp_t = function ;
    void queueTaskCpp(taskCpp_t task, uint8_t* data, uint16_t dataSizeBytes);

    The compiler complains when I simply try to cast my C++ function pointer into a C function pointer:

    void queueTaskCpp(taskCpp_t task, uint8_t* data, uint16_t dataSizeBytes) {
    queueTask((task_t)task, NULL, 0); // Compiler complains!!!
    }

    Does anybody know what I can do to be able to queue taskCpp_t tasks into the same queue as the task_t are queued into?

    L K C M 4 Replies Last reply
    0
    • A arnold_w

      I am writing code for an ARM processor and compiling with GCC inside Eclipse IDE. When the application wants something to get done, it needs to create a task and queue it:

      typedef uint32_t (*task_t)(uint8_t* data, uint16_t dataSizeBytes);
      void queueTask(task_t task, uint8_t* data, uint16_t dataSizeBytes);

      Now I want to be able to queue C++ tasks inside the same task queue:

      #include
      using namespace std;
      using taskCpp_t = function ;
      void queueTaskCpp(taskCpp_t task, uint8_t* data, uint16_t dataSizeBytes);

      The compiler complains when I simply try to cast my C++ function pointer into a C function pointer:

      void queueTaskCpp(taskCpp_t task, uint8_t* data, uint16_t dataSizeBytes) {
      queueTask((task_t)task, NULL, 0); // Compiler complains!!!
      }

      Does anybody know what I can do to be able to queue taskCpp_t tasks into the same queue as the task_t are queued into?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      I don't think you can. A C++ object cannot be used as a simple pointer as the two are totally different.

      1 Reply Last reply
      0
      • A arnold_w

        I am writing code for an ARM processor and compiling with GCC inside Eclipse IDE. When the application wants something to get done, it needs to create a task and queue it:

        typedef uint32_t (*task_t)(uint8_t* data, uint16_t dataSizeBytes);
        void queueTask(task_t task, uint8_t* data, uint16_t dataSizeBytes);

        Now I want to be able to queue C++ tasks inside the same task queue:

        #include
        using namespace std;
        using taskCpp_t = function ;
        void queueTaskCpp(taskCpp_t task, uint8_t* data, uint16_t dataSizeBytes);

        The compiler complains when I simply try to cast my C++ function pointer into a C function pointer:

        void queueTaskCpp(taskCpp_t task, uint8_t* data, uint16_t dataSizeBytes) {
        queueTask((task_t)task, NULL, 0); // Compiler complains!!!
        }

        Does anybody know what I can do to be able to queue taskCpp_t tasks into the same queue as the task_t are queued into?

        K Offline
        K Offline
        k5054
        wrote on last edited by
        #3

        You need to let C++ know that the function call is using C naming conventions, and not C++ name mangling. e.g.

        task.h
        #ifdef __cplusplus
        extern "C" {
        #endif

        typedef uint32_t(*task_t)(uint8_t* data, uint16_t datasizeBytes);
        void queueTask(task_t task, uint8_t* data, uint16_t datasizeBytes);

        #ifdef __cplusplus
        }
        #endif

        Keep Calm and Carry On

        1 Reply Last reply
        0
        • A arnold_w

          I am writing code for an ARM processor and compiling with GCC inside Eclipse IDE. When the application wants something to get done, it needs to create a task and queue it:

          typedef uint32_t (*task_t)(uint8_t* data, uint16_t dataSizeBytes);
          void queueTask(task_t task, uint8_t* data, uint16_t dataSizeBytes);

          Now I want to be able to queue C++ tasks inside the same task queue:

          #include
          using namespace std;
          using taskCpp_t = function ;
          void queueTaskCpp(taskCpp_t task, uint8_t* data, uint16_t dataSizeBytes);

          The compiler complains when I simply try to cast my C++ function pointer into a C function pointer:

          void queueTaskCpp(taskCpp_t task, uint8_t* data, uint16_t dataSizeBytes) {
          queueTask((task_t)task, NULL, 0); // Compiler complains!!!
          }

          Does anybody know what I can do to be able to queue taskCpp_t tasks into the same queue as the task_t are queued into?

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

          You may use a C++ static function as task. That would be equivalent to a standard C function.

          A 1 Reply Last reply
          0
          • A arnold_w

            I am writing code for an ARM processor and compiling with GCC inside Eclipse IDE. When the application wants something to get done, it needs to create a task and queue it:

            typedef uint32_t (*task_t)(uint8_t* data, uint16_t dataSizeBytes);
            void queueTask(task_t task, uint8_t* data, uint16_t dataSizeBytes);

            Now I want to be able to queue C++ tasks inside the same task queue:

            #include
            using namespace std;
            using taskCpp_t = function ;
            void queueTaskCpp(taskCpp_t task, uint8_t* data, uint16_t dataSizeBytes);

            The compiler complains when I simply try to cast my C++ function pointer into a C function pointer:

            void queueTaskCpp(taskCpp_t task, uint8_t* data, uint16_t dataSizeBytes) {
            queueTask((task_t)task, NULL, 0); // Compiler complains!!!
            }

            Does anybody know what I can do to be able to queue taskCpp_t tasks into the same queue as the task_t are queued into?

            M Offline
            M Offline
            Mircea Neacsu
            wrote on last edited by
            #5

            To show how different the two things are try this:

            int main ()
            {
            taskCpp_t t;
            cout << "taskCpp_t size: " << sizeof (taskCpp_t) << '\n';
            cout << "task_t size: " << sizeof (task_t) << '\n';
            }

            I get:

            taskCpp_t size: 64
            task_t size: 8

            but it is up the compiler what a function object should contain.

            Mircea

            1 Reply Last reply
            0
            • C CPallini

              You may use a C++ static function as task. That would be equivalent to a standard C function.

              A Offline
              A Offline
              arnold_w
              wrote on last edited by
              #6

              I'm hoping to be able to use the function like this:

              queueTaskCpp([](uint8_t* data, uint16_t dataSizeBytes) {
              // My function implementation in PURE C (no C++) goes here...
              }, NULL, 0);

              I would assume the above function implementation would become static, right? God forbid if the function gets implemented on the stack, obviously that would mean I can't use this concept at all since the function implementation on the stack will most likely be corrupt when it's time to execute the queued function. I'm not planning to use any classes or object orientedness, I'm only using C++ to be able to pass anonymous functions like this.

              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