Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8986673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:36:22+00:00 2026-06-15T21:36:22+00:00

I have a following code in Fortran and with C master. It gives sigsegv,

  • 0

I have a following code in Fortran and with C “master”. It gives sigsegv, but I do not understand why. It is supposed to take a pointer from the master C program convert it to Fortran pointer format, and read out the data C pointer was pointing to. Nothing fancy, but still a SigSegv. This is BTW the only way I see to circumvent lack of interoperability of Fortran multidimensional arrays with… (yeah, C does not have multidimensional arrays).

The Fortran part:

module ret
    implicit none
    integer, pointer :: a(:,:)
end module ret

module func
    implicit none
    contains

    subroutine initialize(cp) bind(c,name='initialize')
        use ret
        use iso_c_binding
        implicit none
        type(c_ptr) :: cp

        call c_f_pointer(cp,a,[5,5])

    end subroutine initialize

    subroutine printa bind(c,name='printa')
       use ret
       implicit none
       integer :: i,j

       do i=1,5
         do j=1,5
            print *,i,j,a(i,j)
         end do
       end do
    end subroutine printa
end module func

The C Part:

#include<stdio.h>
void initialize( void *);
void printa();

int main()
 {    
  int *tab;
  tab = (int *)malloc(25 * sizeof (int *));
  int i;
  for(i=0;i<25;++i)
   tab[i]=i;
  initialize(tab);
  printa();
  printf("ok\n");
  return 0;
 }

The SIGSEGV occurs at line print *,i,j,a(i,j) in the Fortran part

EDIT:
I have changed “integer*8” back into “integer”
and do i=1,10 back into do i=1,5. For this version the quesion is still valid

SIGSEGV appears for i=1, and j=1

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T21:36:23+00:00Added an answer on June 15, 2026 at 9:36 pm

    I am adding this as an answer just in case someone else comes here with a problem, similar to yours.

    There are two problems in the code shown. First, you are allocating memory for integer pointers as noted by Klas Lindbäck. This is not the source of the problem since pointers are at least the size of int or wider than that, but nevertheless the correct data type should be provided to the sizeof operator in order to prevent overuse of the resources available.

    The second problem is that in Fortran arguments are passed by reference. This means that when one calls Fortran routines from C, then the address-of operator & should be used. Also constant should be placed in separate constant variables and also passed by address. With the previous problem corrected, the C code should be:

    tab = malloc(25 * sizeof (int)); // int instead of int *
    int i;
    for(i = 0; i < 25; ++i)
      tab[i] = i;
    initialize(&tab);                // pass tab by address
    

    Fortran 2003 introduced the VALUE attribute that can be applied to dummy routine arguments in order to make them pass-by-value instead. If your Fortran compiler implements this F2003 feature, then you might change the declaration of the cp dummy argument of initialize to read:

    type(c_ptr), value :: cp
    

    If you do this, you would not need to pass tab by reference.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following FORTRAN code which I need to convert to C or
I have following code for loading image from url in xml parsing endElement method
I have following code working nicely on all browsers but IE 7... <script type=text/javascript>
I have following code - methods to read XML files. But it works very
I have a program where code in C, C++ and Fortran has been compiled
The following Fortran Code: module Mod implicit none TYPE derivedtype procedure(procInterface),POINTER,PASS::f END TYPE derivedtype
i have following code, details.php working fine. but when i access gotolink.php its directing
I have following code snippet and i am trying to evaluate the time take
I have following code.. $query = SELECT quote, author FROM quotes ORDER BY id
I have following code in initialization im = imread('Image02.tif'); figure(); imagesc(im); colormap(gray); [hImage hfig

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.