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 659909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:05:51+00:00 2026-05-13T23:05:51+00:00

I’ve been trying to use SWIG to wrap around a simple library that uses

  • 0

I’ve been trying to use SWIG to wrap around a simple library that uses ioctl() to populate a structure like the following:

struct data
{
  header* hdr;
  void* data;
  size_t len;
};

data is a pointer to a buffer, len is the length of that buffer.

I’m unable to figure out how to convert data to a Python string (or array). Furthermore, I need a way to free that buffer in the destructor. Any suggestions are appreciated.

  • 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-13T23:05:52+00:00Added an answer on May 13, 2026 at 11:05 pm

    Since you say “or something else” in the Q’s title — if you choose to use ctypes, you can represent a void* with c_void_p (one of ctypes’ fundamental data types, and access functions such as free and memcpy from the C runtime library (as long as the latter’s available as a DLL / .so dynamic library, but that’s true pretty widely these days). To get a mutable character buffer into which you can memcpy the data, use create_string_buffer.

    Of course you could alternatively use the Python C API — PyByteArray_FromStringAndSize is what you’d use in this case to make a byte array copy from your void* and length (and of course you’d call free directly, when appropriate, since it’s just C code anyway).

    Another possibility to consider is Cython, a Python-like language designed to write Python extensions and such that the Cython compiler can generate compilable C code from Cython sources – in Cython, your struct would be:

    cdef struct data:
        void* hdr
        void* data
        unsigned int* len
    

    assuming you don’t want to go to the trouble of declaring header (i.e., that only data and len matter to you here) — size_t, I believe, is not part of Cython at this time (I could be wrong, they do keep adding stuff;-), but unsigned int can probably do.

    Sorry, it’s been too long since I used SWIG in earnest (what with all these excellent alternatives) — I’d have skipped the Q were it not for that tempting “or something else” clause in the title;-).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.