Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Many Fortran compilers use internally something called array descriptors - structures that hold the shape of the array (that is the size and range of each dimension as well as pointer to the real data). It allows for the implementation of things like assumed-shape array arguments, array pointers and allocatable arrays to work. What you are accessing through the <code>__bar_MOD_a</code> symbol is the descriptor of the allocatable array and not its data.</p> <p>Array descriptors are compiler-specific and code that relies on specific descriptor format is not portable. Example descriptors:</p> <ul> <li><a href="http://gcc.gnu.org/wiki/ArrayDescriptorUpdate" rel="nofollow">GNU Fortran</a></li> <li><a href="http://software.intel.com/sites/products/documentation/hpc/composerxe/en-us/2011Update/fortran/lin/bldaps_for/common/bldaps_hndl_arrdesc.htm" rel="nofollow">Intel Fortran</a></li> </ul> <p>Note that even those are specific to some versions of those compilers. Intel, for example, states that their current descriptor format is not compatible with the format used in Intel Fortran 7.0.</p> <p>If you look at both descriptors you would see that they are largerly similar and that the first element is a pointer to the array data. So you would be able to easily read the data using <code>double **</code> instead of <code>double *</code>:</p> <pre><code>double **a_descr = (double**)dlsym(bar, "__bar_MOD_a"); ... for(i = 0; i &lt; 4; i++) printf("%f ", (*a_descr)[i]); </code></pre> <p>Once again, this is not portable as the format of those descriptors might change in the future (although I doubt that the data pointer would be moved somewhere else than at the beginning of the descriptor). There is a draft specification that tries to unify all descriptor formats but it is not clear how and when it will be adopted by the different compiler vendors.</p> <p><strong>Edit:</strong> Here is how to use an accessor function that uses <code>C_LOC()</code> from the <code>ISO_C_BINDING</code> module to portably obtain a pointer to the allocatable array:</p> <p>Fortran code:</p> <pre class="lang-none prettyprint-override"><code>module bar use iso_c_binding ... ! Note that the array should be a pointer target real(pa), dimension(:), allocatable, target :: a ... contains ... function getArrayPtr() result(cptr) type(c_ptr) :: cptr cptr = c_loc(a) end function end module </code></pre> <p>C code:</p> <pre><code>... void * (*getArrayPtr)(void); *(void **)(&amp;getArrayPtr) = dlsym(bar, "__bar_MOD_getarrayptr"); ... double *a = (*getArrayPtr)(); for(i = 0; i &lt; 4; i++) printf("%f ", a[i]); ... </code></pre> <p>Result:</p> <pre><code>$ ./prog.x 0.0000000000000000 3.1400000000000001 6.2800000000000002 9.4199999999999999 0.000000 3.140000 6.280000 9.420000 </code></pre>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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