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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:21:37+00:00 2026-06-03T17:21:37+00:00

I have a C function like this: void get_data(const obj_t *obj, short const **data,

  • 0

I have a C function like this:

void get_data(const obj_t *obj, short const **data, int *data_len);

I wrote it like this specifically for Swig, since

const short *get_data(const obj_t *obj, int *data_len);

causes trouble, as SWIG’s typemaps aren’t smart enough to associate the data_len with the return value.

In Java I want to be able to call this function like this:

short data[]= mylib.get_data(obj);

But I can’t figure out how to get the array output parameter to become a return value. With Ruby and Python this works fine, as SWIG for those languages supports returning output params as return values (since the languages can have multiple return values).

How can I get this to work with Java?

  • 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-03T17:21:38+00:00Added an answer on June 3, 2026 at 5:21 pm

    I’ve put together the following test header file to demonstrate the problem:

    typedef struct { } obj_t;
    
    const short *get_data(const obj_t *obj, int *data_len) {
      (void)obj;
      static short arr[] = {1,2,3,4,5};
      *data_len = sizeof(arr)/sizeof(*arr);
      return arr;
    }
    

    I’ll talk through the module file I wrote, it starts pretty standard:

    %module test
    
    %{
    #include "test.h"
    %}
    

    Then we set up a typemap for the data_len argument. It doesn’t need to be visible on the Java side since the length will be known by the array, but we do need to arrange some storage for the pointer to point to and we make sure it lasts long enough that we can read it later when returning the array to Java too.

    %typemap(in,numinputs=0,noblock=1) int *data_len {
       int temp_len;
       $1 = &temp_len;
    }
    

    Then we want SWIG to use short[] on the Java side for the return type:

    %typemap(jstype) const short *get_data "short[]"
    %typemap(jtype) const short *get_data "short[]"
    

    and jshortArray in the JNI side – there’s no need to construct a proxy type, so we just pass the returned value straight through:

    %typemap(jni) const short *get_data "jshortArray"
    %typemap(javaout) const short *get_data {
      return $jnicall;
    }
    

    Finally we create a typemap that’s going to create a new array, with size based on the length returned from the function and copy the returned result into the Java array for us. If needed we should free() the real result array here, but in my example it was statically allocated so didn’t need to be freed.

    %typemap(out) const short *get_data {
      $result = JCALL1(NewShortArray, jenv, temp_len);
      JCALL4(SetShortArrayRegion, jenv, $result, 0, temp_len, $1);
      // If the result was malloc()'d free it here
    }
    

    Finally we include the header file for SWIG to wrap, using the typemaps we just wrote:

    %include "test.h"
    

    I tested this with:

    public class run {
      public static void main(String argv[]) {
        System.loadLibrary("test");
        obj_t obj = new obj_t();
        short[] result = test.get_data(obj);
        for (int i = 0; i < result.length; ++i) {
          System.out.println(result[i]);
        }
      }
    }
    

    Which produced:

    1
    2
    3
    4
    5
    

    For reference you could have wrapped:

    void get_data(const obj_t *obj, short const **data, int *data_len);
    

    also though if your function had a way to query the size without setting the array you could wrap this slightly smarter by allocating an array of the correct size on the Java side. To do this you’d want to to write an intermediate function in Java that queried the size, set up the call and then returned the resulting array. This would allow you to use GetShortArrayElements/ReleaseShortArrayElements for a potentially 0 copy call.

    This would work because arrays in Java are basically passed by reference, e.g.:

    public class ret {
      public static void foo(int arr[]) {
        arr[0] = -100;
      }
    
      public static void main(String argv[]) {
        int arr[] = new int[10];
        System.out.println(arr[0]);
        foo(arr);
        System.out.println(arr[0]);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function like this: private void GetRSS(int start, int end) { for
I have a function using strtok like this void f1(char *name) { ... char
I have a function defined like this: public static void ShowAbout(Point location, bool stripSystemAssemblies
I have a function like this in actionscript3 private function uploadFile(event:MouseEvent):void { var uploader:URLRequest
Imagine I have a template function like this: template<typename Iterator> void myfunc(Iterator a, typename
Suppose I have two functions which look like this: public static void myFunction1(int a,
I have function like this: function gi_insert() { if(!IS_AJAX){ $this->load->library('form_validation'); $this->form_validation->set_rules('name', 'Naslov', 'trim|required|strip_tags'); $this->form_validation->set_rules('body',
I have a function like this: MyFunction(double matrix[4][4]) {/*do stuff*/} I am calling this
I have a function like this: def eventcateg_detail(request): ca = EventTypeCategory.objects.values() for i in
I have a function like this function login_redirect($redirect, $vars = array()) { return $redirect;

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.