Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing 4D C Array to FORTRAN Function
    primarykey
    data
    text
    <p>I'm attempting to pass a 4D array, initialized in C, to a FORTRAN function. Following the advise from a previous <a href="https://stackoverflow.com/questions/18881139/passing-multidimensional-c-array-to-fortran-function-for-modification">question</a>, I first allocate the data array as contiguous data with a single malloc, and then allocate pointers to pointers, making them ultimately point inside the data array. This method works for a 3D array. However, when I go to a 4D array I don't get the values expected in FORTRAN. Does something need to change when I malloc the memory for C arrays > 3 dimensions or is there a bug in the alloc_4D_double function?</p> <p>Code Excerpt:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; double f_function(double ****); double ****alloc_4D_double(int wlen, int xlen, int ylen, int zlen, double *ary) { int i,j,k; double ****multiDary = (double****)malloc(wlen*sizeof(double***)); for (i = 0; i &lt; wlen; i++) { multiDary[i] = (double***)malloc(xlen*sizeof(double**)); for (j = 0; j &lt; xlen; j++) { multiDary[i][j] = (double**)malloc(ylen*sizeof(double*)); for (k = 0; k &lt; ylen; k++) { multiDary[i][j][k] = (double*)malloc(zlen*sizeof(double)); } } } for (i = 0; i &lt; wlen; i++) { for (j = 0; j &lt; xlen; j++) { for (k = 0; k &lt; ylen; k++) { multiDary[i][j][k] = &amp;ary[zlen*(k + ylen*(j + xlen*i))]; } } } return multiDary; } int main ( void ) { // create contiguous memory double *data1D = (double*)malloc(2 * 4 * 10 * 10 * sizeof(double)); // map memory locations to allow use of array syntax double ****exposure4D = alloc_4D_double(2, 4, 10, 10, exposure1D); // assign a value data[0][1][4][4] = 0.777; // pass the pointer to FORTRAN f_funtion(data1D) return 0; } </code></pre> <p>and</p> <pre><code>integer(4) function f_function(data) BIND(C) use, intrinsic :: iso_c_binding implicit none ! real(C_DOUBLE) , intent(in), dimension(2,4,10,10) :: data // this value does not evaluate to 0.7777 write(*,*), data(1,2,5,5) // this value does evaluate to 0.7777 write(*,*), data(1,1,9,2) end function f_function </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload