Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3959460
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:43:59+00:00 2026-05-20T02:43:59+00:00

Or maybe, I shouldn’t cast. Here’s what I’m doing: I’m writing a piece of

  • 0

Or maybe, I shouldn’t cast. Here’s what I’m doing:

I’m writing a piece of code that links a Linux device driver to a higher level library. The authors of the library use void * (under a new name via typedef) to store handles to an implementation specific object that describes a communication channel.

The driver I want to connect with the library uses int to store handles to its channels (because they are file descriptors as returned by calls to open()). So, in my code, I get void * passed in from the library and need to call stuff from the driver using an int and vice versa. I. e.:

// somewhere in the library ...
typedef void* CAN_HANDLE;

// ... in my code
CAN_HANDLE canOpen_driver(s_BOARD *board)
{
  int fd;
  // ...
  fd = open(busname, O_RDWR);
  // ...
  return (CAN_HANDLE) fd; // <-- not safe, especially not when converting back.
}

The adapters that others have written actually store some struct etc. somewhere and just cast between pointers, so no size issues arise. In my case, I don’t really want to manage file descriptors, as the OS already does.

On my PC, I think the pointer is larger than the int, so I could bit-twiddle my way out of this, but the code goes into embedded systems, too, and I’m not experienced enough to make any assumptions about the size of types on those machines.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T02:44:00+00:00Added an answer on May 20, 2026 at 2:44 am

    Edit: Of course you don’t need a struct, you can just allocate memory for a plain int.

    CAN_HANDLE canOpen_driver(s_BOARD *board)
    {
      int *fd = malloc(sizeof(int));
      if (fd)
      {
        // ...
        *fd = open(busname, O_RDWR);
        // ...
        return (CAN_HANDLE) fd;
      } 
    
      // failure
      return NULL;
    }
    

    This assumes there’s a matching call to clean up. Something like:

    void canClose_driver(CAN_HANDLE handle)
    {
      int *fd = handle;
      free(fd);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am asking this here in the hope that someone has maybe come across
I have a ViewController class (maybe I shouldn't have named that class that way
i dont know if i should be asking this here (im now thinking maybe
Maybe my title is not the best but here is what I really want.I
Maybe silly question to the JS gurus and ninjas out there but here goes:
I don't know what I am doing wrong or maybe I just haven't thought
Maybe there's a technical/implementation reason for it, but it seems to me that layout
I think that framework shouldn' be a cage for developer, and because Kohana gives
Maybe I am missing something simple here, but it is frustrating me. So what
I have a form with that code: public Form() { initComponents(); try { File

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.