Silverfrost Forums

Welcome to our forums

Derived Type Problem

26 Feb 2016 2:31 #17244

I have discovered a problem with TYPEs in FTN95 version 7.10. In the following code,

TYPE NOTSOGOOD
	CHARACTER*2 CHARS
	INTEGER*4 NUMBER
	END TYPE NOTSOGOOD
TYPE (NOTSOGOOD) NSG(50)

when one references NSG(1)%CHARS, for example, it pick up 4 bytes: CHARS and the 2 low-order bytes of NUMBER. NSG(1)%NUMBER will contain the high-order bytes in the low-order position and NSG(2)%CHARS in the high-order bytes. NSG(2)%CHARS will contain the entire NSG(2)%NUMBER, and so on.

If I 'constrain' the array with the following equivalent code:

TYPE THISWORKS
	UNION
	MAP
		CHARACTER*6 NOTUSED
		END MAP
	MAP
		CHARACTER*2 CHARS
		INTEGER*4 NUMBER
		END MAP
		END UNION
TYPE (THISWORKS) TW(50)

memory is allocated and referenced correctly. There is no problem with TYPEs that contain only numeric or only character fields. They can be mixed, INTEGER*2 and REAL, for example. It's been awhile since I've looked at the forum or made a post so has anyone else discovered this or is it fixed in 7.20?

4 Mar 2016 3:26 #17285

If you do not specify the keyword SEQUENCE in your TYPE, then you cannot guarantee the 'arrangement' of the data in the derived TYPE. It is in the standard to require this keyword to maintain 'ordering'. Otherwise, the compiler can choose whatever ordering it wishes.

This is especially troublesome when passing data to/from other routines. Withing a single routine, the data should always be consistent, regardless of how they are actually 'arranged'.

All of my derived TYPEs now contain the keyword SEQUENCE. Even if I use them 'locally'.

Lesson learned the hard way...

7 Mar 2016 4:28 #17291

Thank you. What I don't know about Fortran 95 would fill a book. But, hey, ask me anything about Fortran IV.

Please login to reply.