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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:14:51+00:00 2026-06-06T00:14:51+00:00

I am developing some SWIG-generated Java bindings for a C library. The library contains

  • 0

I am developing some SWIG-generated Java bindings for a C library. The library contains functions that take parameters of type void *. On the C side these would typically be passed as a pointer to an array of type float or int cast to void *. In the generated Java bindings, this results in methods that take parameters of type SWIGTYPE_p_void.

What is the best way to construct an array of floats/ints in the Java bindings so that they can be passed as type SWIGTYPE_p_void to these methods?

At the moment I am defining a helper function in my example.i file:

void *floata_to_voidp(float f[])
{
    return (void *)f;
}

And then on the Java side doing something like this:

float foo[] = new float[2];
SWIGTYPE_p_void av = null;

// do something with foo

av = example.floata_to_voidp(foo);
example.myfunction(av);

This seems rather ugly, especially as I would also need an inta_to_voidp() etc in my SWIG interface file for each type conversion I want to support.

Is there a way to do this without helper functions and involving less extra code on the Java side to convert data types?

UPDATE (17/6/12): to give additional detail to the question: what I’m trying to do is take a set of C functions, with prototype int foo(const float *data, int N, const void *argv, float *result) and map them to methods on the Java side where an array of arbitrary type can be passed in as argv. Note that argv is const void * and not void *.

  • 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-06-06T00:14:51+00:00Added an answer on June 6, 2026 at 12:14 am

    There’s an alternative to this answer, it’s very different and gives a more natural solution to this problem, closer to what you were looking for originally. The other suggestions were focused on adding overloads (tedious, manual) or making the array_classes implement a common interface one way or another.

    What it overlooks is that Object is a good match for void* in Java most of the time. Even arrays in Java are Objects. This means if you have SWIG map void* to Object it’ll accept as inputs any arrays you might want to pass in. With a bit of care and some JNI we can then get a pointer to the start of that array to pass in to the function. Obviously we need to reject non array Objects with an exception though.

    We still end up writing some (private) helper functions to arrange extraction of the real underlying pointer and release it when done, but the nice thing about this solution is that we only have to do this once and then we end up with a typemap that can be used for any functions which take an array as void* like this.

    I ended up with the following SWIG interface for this solution:

    %module test
    
    %{
    #include <stdint.h>
    
    void foo(void *in) {
      printf("%p, %d, %g\n", in, *(jint*)in, *(jdouble*)in);
    }
    %}
    
    %typemap(in,numinputs=0) JNIEnv *env "$1 = jenv;"
    
    %javamethodmodifiers arr2voidd "private";
    %javamethodmodifiers arr2voidi "private";
    %javamethodmodifiers freearrd "private";
    %javamethodmodifiers freearri "private";
    %inline %{
    jlong arr2voidd(JNIEnv *env, jdoubleArray arr) {
      void *ptr = (*env)->GetDoubleArrayElements(env, arr, NULL);
      return (intptr_t)ptr;
    }
    
    void freearrd(JNIEnv *env, jdoubleArray arr, jlong map) {
      void *ptr = 0;
      ptr = *(void **)&map;
      (*env)->ReleaseDoubleArrayElements(env, arr, ptr, JNI_ABORT);
    }
    
    jlong arr2voidi(JNIEnv *env, jintArray arr) {
      void *ptr = (*env)->GetIntArrayElements(env, arr, NULL);
      return (intptr_t)ptr;
    }
    
    void freearri(JNIEnv *env, jintArray arr, jlong map) {
      void *ptr = 0;
      ptr = *(void **)&map;
      (*env)->ReleaseIntArrayElements(env, arr, ptr, JNI_ABORT);
    }
    %}
    
    
    %pragma(java) modulecode=%{
      private static long arrPtr(Object o) {
        if (o instanceof double[]) {
          return arr2voidd((double[])o);
        }
        else if (o instanceof int[]) {
          return arr2voidi((int[])o);
        }
        throw new IllegalArgumentException();
      }
    
      private static void freeArrPtr(Object o, long addr) {
        if (o instanceof double[]) {
          freearrd((double[])o, addr);
          return;
        }
        else if (o instanceof int[]) {
          freearri((int[])o, addr);
          return;
        }
        throw new IllegalArgumentException();
      }
    %}
    
    %typemap(jstype) void *arr "Object"
    %typemap(javain,pre="    long tmp$javainput = arrPtr($javainput);",post="      freeArrPtr($javainput, tmp$javainput);") void *arr "tmp$javainput"
    
    void foo(void *arr);
    

    This implements it for two array types, there’s a small finite number and you could use fragments or macros to help with this too. Internally SWIG uses a jlong to represent pointers. So for each array type we need a function that returns a pointer for a given array and another one to release it. These are private and part of the module class – nobody other than the module needs to know how this works.

    There’s then two functions which take the Object and use instanceof (ugly, but arrays in Java don’t have any other common base or interface and generics don’t help) and call the correct function to get/release the pointers.

    With these then it’s just two typemaps to set up SWIG to use it for all void *arr arguments. The jstype typemap instructs SWIG to use Object for void* in these cases. The javain typemap arranges for a temporary local variable to hold the pointer (in a long) and then for it to be used to make the call and to be cleaned up once the call has succeed or failed.

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

Sidebar

Related Questions

I am developing some client side Javascript that is using some JSON web services
I'm developing some tests for the add_signed MPL class that converts the type to
I am developing some convenience wrappers around another software package that defines a bash
I am currently developing some user controls so that I can use them at
I'm developing some widgets into a library for internal use at the company I
I am developing some JavaScript that should work with either Prototype.js or JQuery, thus
I have been developing some computer vision tools with openCV, but every time that
I'm developing some sample code. I had some problem using cocos2d. That is the
I am developing some Python programs that I'm running on a remote Ubuntu Linux
I'm developing some lower end code in my system that uses multiple child classes

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.