This would've made a good Stack Overflow question.

Another technique that can be used in cases like this is to declare the extern C function as taking a 'ref' argument:

testit.chpl
-----------
record R {
  var x: int;
}

require "testit.h";

extern proc foo(ref x: int);

var myR = new R(42);
foo(myR.x);
writeln(myR);


testit.h
--------
void foo(int64_t* x);


testit.c
--------
#include <stdio.h>
#include <stdint.h>
#include "testit.h"

void foo(int64_t* x) {
  printf("Got %lld\n", *x);
  *x += 1;
}



On Tue, 15 Aug 2017, Aji, Ashwin wrote:

I figured that this was a bug in my code, so I did not open a bug report in 
Chapel.

Regards,
Ashwin


-----Original Message-----
From: Michael Ferguson [mailto:[email protected]]
Sent: Wednesday, August 02, 2017 7:56 AM
To: Aji, Ashwin <[email protected]>
Cc: [email protected]
Subject: Re: [Chapel-developers] Reference to a record/class field in Chapel

Hi -

I think c_ptrTo(a_obj.value) ought to work. I think this is a bug. Can file a 
bug report with a simple reproducer?

Thanks,

-michael

Hi,

In Chapel, how can I pass a reference (c pointer style) to a field of a
record/class to an external C function.


For example,

extern proc c_func(val_ptr: c_void_ptr);

class A {
   type retType;
   var value: retType;

   // constructors not shown but they are the usual simple ones }

proc foo() {
  var a_obj = A;
  c_func(c_ptrTo(a_obj.value));

  // codegen first creates a copy of a_obj and then passes pointer to
the copy¹s value field.

  // A a_obj_copy = a_obj; // copy by value
  // c_func(&(c_obj_copy.value));
  // But, I want the below:

  // c_func(&(c_obj.value));
}

Regards,
Ashwin




------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Chapel-developers mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/chapel-developers
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Chapel-developers mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/chapel-developers

Reply via email to