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 8947699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:49:57+00:00 2026-06-15T12:49:57+00:00

I allocated value to status array like this : status[i] += 1; and I

  • 0

I allocated value to status array like this :

status[i] += 1;

and I like to access to this array from fortran
how can I access to this array?
for example I want to change the value of STAT from fortran like this :

STAT(2)=3

is this possible?

c source

#include <stdio.h>
#include <stdlib.h>
#include <sys/shm.h>
#include <sys/stat.h>

void call_fc_ (int *key, int *addr, int *size, int *status)
{
    int i;
    int shmid;
    void* shared_addr;

    //printf("first ptr = %p\n", *addr);

    shmid = shmget (*key, *size, IPC_CREAT | IPC_EXCL | 0666);
    if (shmid == -1)
    {
        printf("shmget is failed!\n");
        exit(0);
    }
    shared_addr = (void*) shmat(shmid, 0, 0);
    status = (int*)shared_addr;
    //printf("status ptr = %p\n", status);

    int data_size = *size/sizeof(int);

    for(i=0; i<data_size;i++) {
        status[i] += 1;
        printf("%d th value : %d \n", i, status[i]);
    }
}

fortran source

IMPLICIT NONE
INTEGER*8 KEY,SIZE,ADDR
DATA KEY  / 777 /
DATA SIZE / 64 /
!DATA ADDR / Z'b76fb000' /

CALL CALL_FC(KEY, ADDR, SIZE, STAT)

PRINT *, 'stat is : ', STAT

! CAN I ACCESS TO STAT LIKE THIS?
!DO I=1,10
!STAT(I) = STAT(I) + 5
!WRITE (*,*) STAT(I)
!END DO

I have been tested this code and I refered to good answers to this question.
but I got an segmentation fault error when I tried to do like this :

integer(c_int) :: key = 777, ssize = 64, addr
integer, pointer, dimension(:) :: stat
type(c_ptr) :: statptr

!DATA KEY  / 777 /
!DATA SIZE / 64 /


print *, 'before stat size = ', size(stat)
call call_fc(key, addr, ssize, statptr)
!print *, 'statptr = ', statptr
call c_f_pointer(statptr, stat, [ssize])
print *, 'after stat size = ', size(stat)

stat(1) = 111 <==
stat(2) = 222
stat(3) = 333

print *, 'stat : ', stat

can you recognize what the matter is?

  • 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-15T12:49:59+00:00Added an answer on June 15, 2026 at 12:49 pm

    You have to declare STAT somehow. If you start to play with dynamic memory allocation, staying in FORTRAN 77 is hopeless. Maybe someone id able to come up with some solution, but this is the smallest change I found possible. It uses Fortran 2003 interoperability with C.(Maybe Cray pointer solution would be shorter, but non-standard)

    USE ISO_C_BINDING
    
    IMPLICIT NONE
    
    
    INTEGER(C_INT) KEY,SIZE,ADDR,I
    DATA KEY  / 777 /
    DATA SIZE / 64 /
    !DATA ADDR / Z'b76fb000' /
    INTEGER,POINTER  :: STAT(:)
    TYPE(C_PTR) :: STATPTR
    
    CALL CALL_FC(KEY, ADDR, SIZE, STATPTR)
    
    call C_F_POINTER(STATPTR,STAT,(/SIZE/))
    
    PRINT *, 'stat is : '
    DO I=1,SIZE
      PRINT *,STAT(I)
    END DO
    
    ! CAN I ACCESS TO STAT LIKE THIS?
    !DO I=1,10
    !STAT(I) = STAT(I) + 5
    !WRITE (*,*) STAT(I)
    !END DO
    END
    

    I am getting some error from your C part, which I didn’t check. Also I do not know exactly, what the program is supposed to do.

    However I really encourage you to use modern Fortran features. Most important is ISO_C_BINDING for interoperability between C and Fortran. Also forget DATA statements and use variable initialization.

    Quick translation to a more modern Fortran:

      use iso_c_binding
    
      implicit none
    
      interface
        subroutine call_fc(key,addr,size,status) bind(C,name='call_fc_')
          import
          integer(c_int) :: key  !intents should be added
          integer(c_int) :: addr
          integer(c_int) :: size
          type(c_ptr) :: status
        end subroutine
      end interface
    
    
    
      integer(c_int) :: key = 777, size=64,addr,i
      integer(c_int),pointer  :: stat(:)
      type(C_ptr) :: statptr
    
      call call_fc(key, addr, size, statptr)
    
      call c_f_pointer(statptr,stat,(/size/))
    
      print *, 'stat is : ',stat
    
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to extract unique values from my (dynamically allocated) array. I have
I'd need a class like std::auto_ptr for an array of unsigned char*, allocated with
If array sizes can only be a constant value than what does char d_name[...]
First, this is homework, so I can not dynamically allocate memory for an array
What happens if I release a non-allocated object? Example: NSString *value = @hello World!;
I want to store 4 boolean possibilities in a single value. For example, I
I want to display three different values allocated to two different headers, in other
I am storing dynamically allocated class pointers in a vector like below. vector<Test *>
When I have an object allocated not dinamically and I return it by value:
I need an example for compressing a string using GZip in android. I want

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.