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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:57:35+00:00 2026-05-27T06:57:35+00:00

I’m having issues passing in an appropriate Java data type for the fetchByType C

  • 0

I’m having issues passing in an appropriate Java data type for the fetchByType C function’s length parameter from my Java implementation (both below). On the C side, the *length parameter is a pointer to a length variable (output) to indicate the size of the data to be returned from the fetchByType C function. I’ve tried to use the SWIG %apply int32_t { int32_t * } and pass an int initialized at 0 from my Java implementation but that fails. I’ve also tried passing in a SWIGTYPE_p_int as shown below, but that also doesn’t work. I don’t have an exception unfortunately as this just crashes.

C Function:

void * fetchByType(struct row_t *result_row, type_t attribute, int32_t *length);

Generated Java from SWIG:

public static String fetchByType(SWIGTYPE_p_result_row_t result_row, type_t attribute, SWIGTYPE_p_int length)

Java Implementation:

SWIGTYPE_p_int length = new SWIGTYPE_p_int();
fetchByType(result_row, attribute, length)
  • 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-27T06:57:35+00:00Added an answer on May 27, 2026 at 6:57 am

    The problem here is that you need to pass something that can be changed into the C function. That means you can’t use a primitive in Java (e.g. int) since Java always passes primitives by value.

    There’s only really one sensible way you can work around this. SWIG in fact already provides a few typemap we can use that passes this as an array instead. Arrays in Java are “objects” and so int[] is passed by reference even though int isn’t. A simplified example based on the function you show above, first test.h:

    inline void fetchByType(int32_t *length) {
      *length = 123;
    }
    

    And a module file, test.i:

    %module test
    
    %{
    #include "test.h"
    %}
    
    // knows about things like int *OUTPUT:
    %include "typemaps.i"
    // knows about int32_t
    %include "stdint.i"
    // let's have an int32_t *OUTPUT for our int32_t *lenght
    %apply int32_t *OUTPUT { int32_t *length }
    
    %include "test.h"
    

    This just makes sure we apply the appropriate *OUTPUT typemap. (stdint.i maps this onto a real C primitive for you). I tested with:

    public class run {
      public static void main(String[] argv) {
        int arr[] = new int[1];
        test.fetchByType(arr);
        System.out.println(arr[0]);
      }
    }
    

    It’s a bit of a hack1, but it’s usable. Another approach might be to use a java.lang.Integer, which also represents an int and is a proper Object so gets passed by reference. The problem there is that java.lange.Integer is immutable however and I’m not actually sure enough how that works on the JNI side to provide an example of this.

    1 Other languages SWIG supports alter the return type for some typemaps and return a tuple or similar for OUTPUT arguments, but that’s not really feasible with Java.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to construct a data frame in an Rcpp function, but when I
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I would like to count the length of a string with PHP. The string
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string

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.