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

  • Home
  • SEARCH
  • 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 138835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:19:39+00:00 2026-05-11T07:19:39+00:00

I have a C interface that looks like this (simplified): extern bool Operation(void **

  • 0

I have a C interface that looks like this (simplified):

extern bool Operation(void ** ppData); extern float GetFieldValue(void* pData); extern void Cleanup(p); 

which is used as follows:

void * p = NULL; float theAnswer = 0.0f; if (Operation(&p)) {    theAnswer = GetFieldValue(p);    Cleanup(p); } 

You’ll note that Operation() allocates the buffer p, that GetFieldValue queries p, and that Cleanup frees p. I don’t have any control over the C interface — that code is widely used elsewhere.

I’d like to call this code from Python via SWIG, but I was unable to find any good examples of how to pass a pointer to a pointer — and retrieve its value.

I think the correct way to do this is by use of typemaps, so I defined an interface that would automatically dereference p for me on the C side:

%typemap(in) void** {    $1 = (void**)&($input); } 

However, I was unable to get the following python code to work:

import test p = None theAnswer = 0.0f if test.Operation(p):    theAnswer = test.GetFieldValue(p)    test.Cleanup(p) 

After calling test.Operation(), p always kept its initial value of None.

Any help with figuring out the correct way to do this in SWIG would be much appreciated. Otherwise, I’m likely to just write a C++ wrapper around the C code that stops Python from having to deal with the pointer. And then wrap that wrapper with SWIG. Somebody stop me!

Edit:

Thanks to Jorenko, I now have the following SWIG interface:

% module Test  %typemap (in,numinputs=0) void** (void *temp) {     $1 = &temp; }  %typemap (argout) void** {     PyObject *obj = PyCObject_FromVoidPtr(*$1, Cleanup);     $result = PyTuple_Pack(2, $result, obj); } %{ extern bool Operation(void ** ppData);  extern float GetFieldValue(void *p);  extern void Cleanup(void *p); %}  %inline  %{      float gfv(void *p){ return GetFieldValue(p);}  %}   %typemap (in) void* {     if (PyCObject_Check($input))     {         $1 = PyCObject_AsVoidPtr($input);     } } 

The python code that uses this SWIG interface is as follows:

import test  success, p = test.Operation() if success:    f = test.GetFieldValue(p) # This doesn't work     f = test.gvp(p) # This works!     test.Cleanup(p)  

Oddly, in the python code, test.GetFieldValue(p) returns gibberish, but test.gfv(p) returns the correct value. I’ve inserting debugging code into the typemap for void*, and both have the same value of p! The call Any ideas about that?

Update: I’ve decided to use ctypes. MUCH easier.

  • 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. 2026-05-11T07:19:40+00:00Added an answer on May 11, 2026 at 7:19 am

    I agree with theller, you should use ctypes instead. It’s always easier than thinking about typemaps.

    But, if you’re dead set on using swig, what you need to do is make a typemap for void** that RETURNS the newly allocated void*:

    %typemap (in,numinputs=0) void** (void *temp) {     $1 = &temp; }  %typemap (argout) void** {     PyObject *obj = PyCObject_FromVoidPtr(*$1);     $result = PyTuple_Pack(2, $result, obj); } 

    Then your python looks like:

    import test success, p = test.Operation() theAnswer = 0.0f if success:    theAnswer = test.GetFieldValue(p)    test.Cleanup(p) 

    Edit:

    I’d expect swig to handle a simple by-value void* arg gracefully on its own, but just in case, here’s swig code to wrap the void* for GetFieldValue() and Cleanup():

    %typemap (in) void* {     $1 = PyCObject_AsVoidPtr($input); } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 194k
  • Answers 194k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I would strongly discourage you from the proposed branching option.… May 12, 2026 at 6:42 pm
  • Editorial Team
    Editorial Team added an answer I have this in my ~/.zshrc case $TERM in *xterm*|rxvt|(dt|k|E)term)… May 12, 2026 at 6:42 pm
  • Editorial Team
    Editorial Team added an answer What happens on the server beyond the http interface is… May 12, 2026 at 6:42 pm

Related Questions

I've written a fairly simple little C# web service, hosted from a standalone EXE
I am looking for an open source library to parse and execute formula/functions in
I have a problem with WCF. I think I understand what the issue is,
I am currently trying to create a menu system for a game and cannot

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.