{"id":1985,"date":"2014-10-20T12:13:17","date_gmt":"2014-10-20T11:13:17","guid":{"rendered":"http:\/\/roboblog.fatal-fury.de\/?p=1985"},"modified":"2014-10-20T12:13:17","modified_gmt":"2014-10-20T11:13:17","slug":"howto-allocate-array-in-c-and-pass-it-back-to-fortran","status":"publish","type":"post","link":"http:\/\/roboblog.fatal-fury.de\/?p=1985","title":{"rendered":"howto allocate array in C++ and pass it back to fortran"},"content":{"rendered":"<p>test.cpp<\/p>\n<pre><code>\r\n#include < stdlib.h >\r\n\r\n\/\/ fortran can only call C routines\r\nextern \"C\" {\r\n  \/\/ we need the address of the fortran pointer thus a pointer of a pointer.\r\n  void myC_func(int **arr) {   \r\n    \/\/ we cannot use new() here because fortran deallocate() use free() instead of delete[]\r\n    *arr = (int*) malloc(12*sizeof(int));\r\n    for(int i=0; i < 12; i++) {\r\n      (*arr)[i] = i;\r\n    }\r\n  }\r\n}\r\n<\/code><\/pre>\n<p>test.F90<\/p>\n<pre><code>\r\n! https:\/\/gcc.gnu.org\/onlinedocs\/gcc-4.6.2\/gfortran\/Working-with-Pointers.html\r\n\r\nprogram main\r\n  use iso_c_binding\r\n  implicit none\r\n  \r\n  interface\r\n    subroutine my_routine(arr) bind(c,name='myC_func')\r\n      import :: c_ptr\r\n      ! If a pointer is a dummy-argument of an interoperable procedure, \r\n      ! it usually has to be declared using the VALUE attribute. \r\n      ! void* matches TYPE(C_PTR), VALUE, while TYPE(C_PTR) alone matches void**. \r\n      type(c_ptr), intent(out) :: arr\r\n    end subroutine\r\n  end interface\r\n  \r\n  type(c_ptr) :: cptr\r\n  integer,pointer :: fptr(:)\r\n  \r\n  ! allocate and fill cptr\r\n  call my_routine(cptr)\r\n  ! convert it to fortran pointer\r\n  call c_f_pointer(cptr, fptr, [12])\r\n  \r\n  if(associated(fptr)) write(*,*) \"associated\"\r\n  write(*,*) fptr\r\n  deallocate(fptr)\r\n  \r\nend program main\r\n<\/code><\/pre>\n<pre>\r\ng++ -Wall -Wextra -c test.cpp \r\ngfortran -Wall -Wextra -fcheck=all test.F90 test.o\r\n\r\nvalgrind .\/a.out \r\n==5256== Memcheck, a memory error detector\r\n==5256== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.\r\n==5256== Using Valgrind-3.9.0 and LibVEX; rerun with -h for copyright info\r\n==5256== Command: .\/a.out\r\n==5256== \r\n associated\r\n           0           1           2           3           4           5           6           7           8           9          10          11\r\n==5256== \r\n==5256== HEAP SUMMARY:\r\n==5256==     in use at exit: 0 bytes in 0 blocks\r\n==5256==   total heap usage: 22 allocs, 22 frees, 11,874 bytes allocated\r\n==5256== \r\n==5256== All heap blocks were freed -- no leaks are possible\r\n==5256== \r\n==5256== For counts of detected and suppressed errors, rerun with: -v\r\n==5256== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>test.cpp #include < stdlib.h > \/\/ fortran can only call C routines extern \"C\" { \/\/ we need the address of the fortran pointer thus a pointer of a pointer. void myC_func(int **arr) { \/\/ we cannot use new() here because fortran deallocate() use free() instead of delete[] *arr = (int*) malloc(12*sizeof(int)); for(int i=0; i [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[14,17,30],"class_list":["post-1985","post","type-post","status-publish","format-standard","hentry","category-allgemein","tag-c","tag-cpp","tag-fortran"],"_links":{"self":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/1985","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1985"}],"version-history":[{"count":5,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/1985\/revisions"}],"predecessor-version":[{"id":1990,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/1985\/revisions\/1990"}],"wp:attachment":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1985"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1985"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1985"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}