Silverfrost Forums

Welcome to our forums

How to use transfer

10 Jun 2011 11:48 #8372

Hello I hope this is my last question! 😉

Is there anyone who can explane the use of transfer in the following case:

    OBJECT('System.String') :: STR
    CHARACTER*6 :: CHR
!
    STR = 'ABCDEF'
    CHR = transfer(STR, CHR)
!
    PRINT *,CHR

The output string are hieroglyphics. So I think, I never get the string 'ABCDF' but the Memory address of the variable (pointer).

I try the following with no changes:

    OBJECT('System.String') :: STR
    CHARACTER*6 :: CHR
    INTEGER*2 LENGTH
!
    LENGTH = 6
    STR = 'ABCDEF'
    CHR = transfer(STR, CHR, LENGTH)
!
    PRINT *,CHR

The outoput is the same as on the top!

What do I wrong? Or how can I get access of the value (string) and not of the address (pointer)?

Greetings from Germany Michael

11 Jun 2011 6:38 #8377

Transfer just copies 'bits' from one variable to another. The target and source variables need to be the same size and it needs to make sense to copy the bits. Think of it as a run-time form of EQUIVALENCE.

As the bit patterns for the string 'ABCDEF' are probably different for the two variable types OBJECT('System.String') and CHARACTER*6 then transfer is not much use in this case. You will just get garbage.

However something like OBJECT('System.Int32') and INTEGER*4 are likely to have the same bit pattern representation.

Transfer isn't very portable. I use it to implement generic function interfaces in Fortran 95 (where transfer is made to 'coerce' a variable into an integer array for passing, and later this is transfered back to the variable type). This is quite portable but it breaks some optimizing compilers. Once we get fully working Fortran 2003 compilers there are better ways to do this.

13 Jun 2011 10:29 #8396

.NET strings are unicode, though I don't know if this is the only problem here.

15 Jun 2011 5:15 #8430

Thanks allot!

It Works!!!

Greetings from Germany Michael

Please login to reply.