Q:Fortran call C parameter passing 
Author Message
 Q:Fortran call C parameter passing

Hi all,

I have a fortran code calling a C function. In Fortran, the 6th
parameter in the calling sequence is declared as,

INTEGER     ISIZE20
PARAMETER   (ISIZE20 = 20)

In C it was declared as int *sizePtr.

The problem was that when *sizePtr is assigned to, a SEGV was generated.
I compared &ISIZE20 in Fortran and sizePtr in C. sizePtr is not pointing
to the address of ISIZE20.

It seems that this is caused by ISIZE20 is a constant PARAMETER.
But how does C function get to know this?

Thanks,

Chai Liang



Wed, 18 Jun 1902 08:00:00 GMT  
 Q:Fortran call C parameter passing

Quote:

> I have a Fortran code calling a C function. In Fortran, the 6th
> parameter in the calling sequence is declared as,

> INTEGER     ISIZE20
> PARAMETER   (ISIZE20 = 20)

> In C it was declared as int *sizePtr.

> The problem was that when *sizePtr is assigned to,

You can't assign to a parameter.  That makes no sense.

Quote:
> a SEGV was generated.
> I compared &ISIZE20 in Fortran and sizePtr in C. sizePtr is not pointing
> to the address of ISIZE20.

> It seems that this is caused by ISIZE20 is a constant PARAMETER.
> But how does C function get to know this?

You should not confuse parameters with variables; they are very different
things.  There is no reason for ISIZE20 to necessarily even *HAVE* an
address.  As a parameter, it represents just the number 20, not
some address.

Think of a parameter as being a compile-time construct.  Since you
mention C, I'd draw analogy with a #define in the C preprocessor.
If you did a

  #define ISIZE20 20

then what would you say about the address of ISIZE20?  The question
is meaningless - it doesn't have an address.

Depending on how it is used, the compiler *MIGHT* find it convenient
to define some address that contains the value 20 and use that in
some contexts.  But this is certainly not guaranteed to be a unique
address.  There might be multiple addresses where the value is
stored (for example, one for each of several places where the
parameter is used), or there might be none.

--
Richard Maine



Wed, 18 Jun 1902 08:00:00 GMT  
 Q:Fortran call C parameter passing
Dear Chai:

Quote:

> Hi all,

> I have a Fortran code calling a C function. In Fortran, the 6th
> parameter in the calling sequence is declared as,

> INTEGER     ISIZE20
> PARAMETER   (ISIZE20 = 20)

> In C it was declared as int *sizePtr.

> The problem was that when *sizePtr is assigned to, a SEGV was generated.
> I compared &ISIZE20 in Fortran and sizePtr in C. sizePtr is not pointing
> to the address of ISIZE20.

> It seems that this is caused by ISIZE20 is a constant PARAMETER.
> But how does C function get to know this?

> Thanks,

> Chai Liang

    In Fortran, the PARAMETER attribute or statement is used to define a NAMED
CONSTANT.  If you pass the named constant as an actual argument, then any
assignment to the dummy argument modifies the value of a named constant.
Obviously, you can't modify the value of a constant.

    As Richard Maine pointed out, named constants do not have addresses in
some Fortran implementations.  In some other implementations, named constants
DO have addresses but they are located in write-protected (AKA read-only)
areas of memory.  If you attempt to modify them, you will get weird and FATAL
run-time errors.  I once did what you did in DEC Fortran under VMS back in
1985.  I got a {*filter*} program crash along with an error message something like,
"Attempt to modify read-only program section".  It took me a while to figure
out exactly what mistake I had made.

--
----------
Sincerely,

Elmbrook Computer Services Voice Phone: (414) 783-5869
17130 W. Burleigh Place  Fax Phone:   (414) 783-5928
Brookfield, WI   53005  Disclaimer:  These opinions are mine alone.
USA    They do NOT represent any organization.

"They that can give up essential liberty to obtain a little temporary
    safety deserve neither liberty nor safety."  -- Benjamin Franklin (1759)



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. calling C++ function from Fortran: parameter passing

2. Call a C5 exe from other program with passed parameters

3. OCXs: Parameter passing and calling method...

4. Calling conventions, passing parameters from asm to c

5. Calling JPython functions and passing parameters for Java

6. Passing parameter when loading a Fortran program

7. HELP: fortran parameter passing

8. FORTRAN parameter passing

9. FORTRAN value passed parameters revisited

10. Passing a parameter Rexx to JCL and return a parameter to JCL

11. language rules question: using a (passed in) parameter to define a parameter

12. cannot use parameters from subroutine-header when calling Lahey Fortran 90 DLL from Delphi 3

 

 
Powered by phpBB® Forum Software