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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:04:39+00:00 2026-05-31T10:04:39+00:00

I think title says what I need. I know we can use asd function

  • 0

I think title says what I need. I know we can use “asd” function to do this, but for some reasons I need to do the allocation in Fortran (i.e. in subroutine “asd_”). Here is the C code:

#include <stdio.h>

void asd(float **c) {
  *c = (float *) malloc (2*sizeof(float));
  **c =123;
  *(*c+1)=1234;
}

void asd_(float **c);

main () {
  float *c;
  asd_(&c);
// asd(&c); would do the job perfectly
  printf("%f %f \n",c[0],c[1]);
  return 0;
}

And here is the Fortran code:

  subroutine asd(c)

  implicit none

  real, pointer, allocatable ::c(:)

  print *, associated(c)
  if(.not. associated(c))  allocate(c(2))

  end subroutine 

This randomly gives segmentation fault. Any help would be appreciated.

  • 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-05-31T10:04:40+00:00Added an answer on May 31, 2026 at 10:04 am

    Here is also another solution, if you want to use Fortran intrinsic types. This was my case, since I needed to call routines from an external library, using the pre-specified data types. This is basically done with a wrapper Fortran subroutine. Here is the C code:

    void mywrap_(void **);
    void myprint_(void *);
    
    main () {
      void *d;
      mywrap_(&d);
      myprint_(d);
      return 0;
    }
    

    And here is the wrapper:

      subroutine mywrap(b)
      implicit none
      include "h.h"     
      type(st), target, save :: a
      integer, pointer :: b
      interface 
         subroutine alloc(a)
            include "h.h"
            type(st) a
         end subroutine alloc
      end interface
    
      call alloc(a)
      b => a%i
      end
    

    And the Fortran codes:

      subroutine alloc(a)
      implicit none 
      include "h.h"
      type(st) a
    
      a%i = 2
      a%r = 1.5
      if (allocated(a%s)) deallocate(a%s)
      allocate(a%s(2))
      a%s(1) = 1.23
      a%s(2) = 1234
      end
      !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
      subroutine myprint(a)
      implicit none
      include "h.h"     
      type(st) a
    
      print *,"INT: ", a%i
      print *,"REAL: ", a%r
      print *,"ALLOC: ", a%s
      end
    

    And the header file “h.h”:

      type st
         sequence
         integer i
         real r
         real, allocatable :: s(:)
      end type
    

    Note, this way all the objects are opaque in the C.

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

Sidebar

Related Questions

I think the title says it, as well...but of course some more explanation assume
OK, I don't think the title says it right... but here goes: I have
As title says, I'm currently doing some small hacks with pointers in C++, but
The title says enough I think. I have a full quality BufferedImage and I
I think the title pretty much says it all... I'm looking to implement an
i think my whole questions is inside title bar =] but i'll explain little
I think I summed up the question in the title. Here is some further
I know there are other questions that are very similar to this, but their
I think my title about says it all. During the __construct() -tion of $classA
As title says, I'm wondering if stacking subviews can slow down an iPhone application.

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.