Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segmentation fault #8

Closed
kimtn-bitmark opened this issue Aug 27, 2015 · 7 comments
Closed

Segmentation fault #8

kimtn-bitmark opened this issue Aug 27, 2015 · 7 comments

Comments

@kimtn-bitmark
Copy link

I trying to follow the sample in your document but i got this error "Segmentation fault"

C file

void test(const int a[]) {
  int i = 0;
  FOR(i, sizeof(a)) {
    printf("input: %d\n", a[i]);
  }
}

js file

var ffi = require('ffi');
var ref = require('ref');
var ArrayType = require('ref-array');
// typedef
var int = ref.types.int

// define the "int[]" type
var IntArray = ArrayType(int)
var mylibrary = ffi.Library('./mylibrary', {
    'test': ['void', [IntArray]]
});

var a = new IntArray(5) // by length
a.length // 5
a[0] = 0
a[1] = 1
a[2] = -1
a[3] = 2
a[4] = -2

mylibrary.test(a);

Someone else can correct me?

@TooTallNate
Copy link
Owner

I don't think you can get the sizeof() like that in C... I think instead you're going to have to pass in the length of the array as a second argument. So maybe something like this:

void test(const int a*, int length) {
  int i = 0;
  FOR(i, length) {
    printf("input: %d\n", a[i]);
  }
}

and then invoke it with FFI doing something like:

mylibrary.test(a, a.length);

@TooTallNate
Copy link
Owner

There's a related clang warning as well:

./c.c:8:19: warning: sizeof on array function parameter will return size of 'const int *' instead of 'const int []' [-Wsizeof-array-argument]

@kimtn-bitmark
Copy link
Author

Hi @TooTallNate, thanks for your answer.

I tried base on your answer but it does not work. I still got "Segmentation fault" error

// gcc -shared -fpic mylibrary.c -o mylibrary.so

#include <stdint.h>
#include <stdio.h>
#define FOR(i,n) for (i = 0;i < n;++i)

void test(const int *a, const int length) {
  int i;
  FOR(i, length) {
    printf("input: %d\n", a[i]);
  }
}

@TooTallNate
Copy link
Owner

Can you show me your JS code as well?

@kimtn-bitmark
Copy link
Author

My js API

var ffi = require('ffi');
var ref = require('ref');
var ArrayType = require('ref-array');

// typedef
var int = ref.types.int

// define the "int[]" type
var IntArray = ArrayType(int)

module.exports = function() {
  var testAPI = ffi.Library('./mylibrary', {
    'test': ['void', [IntArray, int]],
  })

  return testAPI;
}()

my test file

var ref = require('ref');
var ArrayType = require('ref-array');
var testAPI = require('./testAPI');

// typedef
var int = ref.types.int

// define the "int[]" type
var IntArray = ArrayType(int)

var a = new IntArray(5) // by length
a.length // 5
a[0] = 0
a[1] = 1
a[2] = -1
a[3] = 2
a[4] = -2

var test = testAPI.test(a, a.length);

i'm using

gcc=v4.6.3
node=v0.12.7
ref=v1.0.2
ref-array=v1.1.1

@TooTallNate
Copy link
Owner

That looks like it should be ok to me. Let me try running it locally and I'll get back to you.

@kimtn-bitmark
Copy link
Author

Hi @TooTallNate how it is going.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants