Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Fortran Programming Language

allocating memory from C


Hi,

I have the suspicion that it might not be possible at all but here it
goes.

>From a fortran program I'd like to call a C function, passing to it as

arguments Fortran pointers, and allocate memory to them from inside
the C function.

In pseude code:

    subroutine doit()

    character, pointer ph

    integer hlen,ret

    ret = foo(ph, hlen)

    end

where ph has entirely being allocated in C and hlen is the size of the
array

Is this possible? if so how?

Many thanks in advance

Francis

igl@gmail.com wrote:
> I have the suspicion that it might not be possible at all but here it
> goes.
>>From a fortran program I'd like to call a C function, passing to it as
> arguments Fortran pointers, and allocate memory to them from inside
> the C function.
> In pseude code:
>     subroutine doit()
>     character, pointer ph
>     integer hlen,ret
>     ret = foo(ph, hlen)
>     end
> where ph has entirely being allocated in C and hlen is
> the size of the array
> Is this possible? if so how?

It might be possible within the Fortran 2003 C interoperability
feature, using the C_F_POINTER function.  It might be that it
would require, and it seems a little easier, to call one C
function to determine the amount to allocate, allocate from
a Fortran subroutine or function, then call the C function
to do the rest of the work.

Otherwise, it can likely be done non-portably if you can find
the structure of Fortran pointers for your implementation.  You
can then pass a pointer as an ALLOCATABLE argument to the C
function, which can then extract the address and bounds for
the pointer.  I would then call a Fortran subroutine from
the C function, again with the pointer as an ALLOCATABLE
actual argument to do the actual allocation.  It won't
be completely portable, but not so far off.

To be even less portable, you can call the Fortran library
pointer allocation routine from C.

-- glen

As another response hints, this is commonly done in legacy applications,
and should be possible to accomplish somewhat portably with f2003 C
interop.  If your compiler doesn't support C interop, you would need it
to support Cray pointers.  Of course, Fortran has supported its own
ALLOCATABLE functionality for over 15 years, so it would be somewhat odd
to do this without more justification than you have given.
Tim Prince wrote:

(snip)

> As another response hints, this is commonly done in legacy applications,
> and should be possible to accomplish somewhat portably with f2003 C
> interop.  If your compiler doesn't support C interop, you would need it
> to support Cray pointers.  Of course, Fortran has supported its own
> ALLOCATABLE functionality for over 15 years, so it would be somewhat odd
> to do this without more justification than you have given.

My guess is that the C function determines the size that the arrays
need to be.  In that case, an easier solution is to call a C function
that determines the sizes and returns, allocate in Fortran, then call
the C function that uses the arrays.   It would be a little easier
if C had ENTRY, but maybe two separate functions will work.

-- glen

igl@gmail.com wrote:

| Hi,
|
| I have the suspicion that it might not be possible at all but here it
| goes.
|
|| From a fortran program I'd like to call a C function, passing to it as
| arguments Fortran pointers, and allocate memory to them from inside
| the C function.
|
| In pseude code:
|
|    subroutine doit()
|
|    character, pointer ph
|
|    integer hlen,ret
|
|    ret = foo(ph, hlen)
|
|    end
|
|
| where ph has entirely being allocated in C and hlen is the size of the
| array
|
| Is this possible? if so how?

As others pointed out, not really/quite/yet, but I have some "cheating"
code handy (if you don't mind portability). Which compiler do you have?

--
 Jugoslav
___________
www.xeffort.com

Please reply to the newsgroup.
You can find my real e-mail on my home page above.

On Apr 23, 11:58 pm, Tim Prince <timothypri@sbcglobal.net> wrote:

The C api I'm wrapping already allocates it's memory. I thought I
could just pass that pointer of the memory space to the fortran code.
Otherwise I'll have to double allocate and copy.

igl@gmail.com wrote:

(snip)

> The C api I'm wrapping already allocates it's memory. I thought I
> could just pass that pointer of the memory space to the fortran code.
> Otherwise I'll have to double allocate and copy.

It is unusual to have a C function allocate memory and return
(a pointer to) the result.  The caller is then responsible
for free()ing that memory, a favorite cause for memory leaks.

But if you do, that is what Fortran 2003's C_F_POINTER
is for.  I am not sure about deallocation, though.
I don't see anything saying that DEALLOCATE can be used
on such a pointer, so it might be that free() needs to
be called on the C pointer.

-- glen

On Apr 24, 8:49 am, "Jugoslav Dujic" <jdu@yahoo.com> wrote:

mmh, well I'm willing to wave that if it really gets me there. The
compiler is ifort.

Thanks a lot for the help!

On Apr 23, 6:08 pm, glen herrmannsfeldt <g@ugcs.caltech.edu> wrote:

Thanks a lot for your response. The ALLOCATABLE solution might just
work. I'll do some digging on my fortran compiler documentation to see
if they mention anything about how the ALLOCATABLE pointers map in C.

Thanks again for you help!

> for free()ing that memory, a favorite cause for memory leaks.

correct!, I'll be providing an specific free function for the C code

> But if you do, that is what Fortran 2003's C_F_POINTER
> is for.

Unfortunately my compiler (ifort) doesn't implement Fortran 2003

-Fran

igl@gmail.com wrote:

| On Apr 24, 8:49 am, "Jugoslav Dujic" <jdu@yahoo.com> wrote:

|| igl@gmail.com wrote:

||
|| As others pointed out, not really/quite/yet, but I have some "cheating"
|| code handy (if you don't mind portability). Which compiler do you have?
| mmh, well I'm willing to wave that if it really gets me there. The
| compiler is ifort.
|
| Thanks a lot for the help!

<replied by e-mail>

--
 Jugoslav
___________
www.xeffort.com

Please reply to the newsgroup.
You can find my real e-mail on my home page above.

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc