Sharing of memory between C and SV

Hi,
I am experimenting couple of things with DPI. I have question on Memory Management between SV and C( I have read sec 35.5.1.4 in LRM 2012).
In the below code, I have array of size 8. Initializing all the 8 location with value 5. I pass the array to C where I print the values and modify them to 6. When I re-enter SV and print the values of the array, I see original values(5) being printed.
Question is - Does this mean when ever we pass an array from SV to C or C (pointer) to SV, the array is passed as value and not as reference ? Is there a way to pass array as reference ?

I tried doing the other way(malloc in C → initialize->pass the pointer to SV…) and ended with the same result.
Thanks is advance for your answers.


module tb;
//EXP2
import "DPI-C" context function void exp2_c(int arr[8]);
int arr[8];

  initial begin
    //EXP2: Malloc experiment
    foreach(arr[i]) arr[i] = 5;
    foreach(arr[i]) $display("araray[%0d] = %0d",i,arr[i]);
    exp2_c(arr);
    for(int i=0;i<8;i++) begin
        $display("araray[%0d] = %0d",i,arr[i]);
    end
  end
endmodule

//C code
void exp2_c(svOpenArrayHandle arr_h) {
  int i,*ptr;
  ptr = (int *) arr_h;
  for(i=0;i<8;i++) {
    printf("C: ptr[%0d] = %0d\n",i,ptr[i]);
    ptr[i] = 6;
  }
}


In reply to Omkar:

The C prototype for your function is

void exp2_c(const int* arr);

Because the default argument direction is input, which means copy on entry. You want inout, and the C prototype becomes

void exp2_c(int* arr);

In reply to dave_59:

Dave,
Thanks for the answer.

In reply to Omkar:

how do you make the shared memory sustain its handle(pointer) and content between SV → c calls?

In reply to MichaelSong:

I can think of at least two ways.

You can create static variables in your C code that hold pointers to be shared amongst your calls to C routines.

You can use SystemVerilog’s chandle to pass pointers back and forth.