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;
}
}