STRUCTURE과 RECORD
STRUCTURE과 RECORD
Fortran 90 이전에 벤더가 제공하던 레코드 구조체(record structure) 확장을 GNU Fortran이 어떻게 지원하는지 설명해요. 가능하면 -fdec-structure 플래그로 켜는 이 옛 문법 대신 Fortran 90의 파생 타입(derived type)을 쓰는 게 좋다는 점과, 두 문법을 서로 변환하는 규칙을 정리했어요.
본문
레코드 구조체는 사용자 정의 복합 데이터 타입을 만들기 위한 Fortran 90 이전의 벤더 확장이에요. GNU Fortran에서 레코드 구조체를 지원하려면 -fdec-structure 컴파일 플래그를 켜면 돼요. 선택의 여지가 있다면, 문법이 다른 Fortran 90의 "파생 타입(derived types)"을 쓰는 것이 좋아요.
많은 경우 레코드 구조체는 쉽게 파생 타입으로 변환할 수 있어요. 변환하려면 STRUCTURE /구조체-이름/을 TYPE타입-이름으로 바꿔요. 또한 RECORD /구조체-이름/을 TYPE(타입-이름)으로 바꿔요. 마지막으로 구성 요소 접근에서 점(.)을 퍼센트 기호(%)로 바꿔요.
다음은 이식성이 없는 레코드 구조체 문법을 사용한 코드의 예시예요.
! Declaring a structure named ``item'' and containing three fields: ! an integer ID, an description string and a floating-point price. STRUCTURE /item/ INTEGER id CHARACTER(LEN=200) description REAL price END STRUCTURE ! Define two variables, an single record of type ``item'' ! named ``pear'', and an array of items named ``store_catalog'' RECORD /item/ pear, store_catalog(100) ! We can directly access the fields of both variables pear.id = 92316 pear.description = "juicy D'Anjou pear" pear.price = 0.15 store_catalog(7).id = 7831 store_catalog(7).description = "milk bottle" store_catalog(7).price = 1.2 ! We can also manipulate the whole structure store_catalog(12) = pear print *, store_catalog(12)
이 코드는 다음과 같이 Fortran 90 문법으로 쉽게 다시 쓸 수 있어요.
! ``STRUCTURE /name/ ... END STRUCTURE'' becomes ! ``TYPE name ... END TYPE'' TYPE item INTEGER id CHARACTER(LEN=200) description REAL price END TYPE ! ``RECORD /name/ variable'' becomes ``TYPE(name) variable'' TYPE(item) pear, store_catalog(100) ! Instead of using a dot (.) to access fields of a record, the ! standard syntax uses a percent sign (%) pear%id = 92316 pear%description = "juicy D'Anjou pear" pear%price = 0.15 store_catalog(7)%id = 7831 store_catalog(7)%description = "milk bottle" store_catalog(7)%price = 1.2 ! Assignments of a whole variable do not change store_catalog(12) = pear print *, store_catalog(12)
GNU Fortran은 구조체를 파생 타입처럼 구현하는데, 다음 규칙과 예외를 따라요.
- 구조체는
SEQUENCE속성을 가진 파생 타입처럼 동작해요. 그 외에는 지정자(specifier)를 가질 수 없어요. - 구조체는
%FILL이라는 이름의 특수 필드를 가질 수 있어요. 이 필드는 접근할 수 없지만, 같은 타입의 구성 요소가 그 자리에 선언된 것처럼 공간을 차지하는 익명 구성 요소를 만들어 내요. 정렬(alignment) 목적으로 유용하죠. 예를 들어 다음 구조체는 최소 16바이트를 차지해요.
structure /padded/ character(4) start character(8) %FILL character(4) end end structure
- 구조체는 다른 기호와 이름을 공유할 수 있어요. 예를 들어 다음은 파생 타입에서는 유효하지 않지만 구조체에서는 유효해요.
structure /header/ ! ... end structure record /header/ header
- 구조체 타입은 다른 부모 구조체 안에 중첩되어 선언될 수 있어요. 문법은 다음과 같아요.
structure /type-name/ ... structure [/<type-name>/] <field-list> ...
타입 이름은 생략될 수 있는데, 이 경우 구조체 타입 자체는 익명이 되고 같은 타입의 다른 구조체는 인스턴스화할 수 없어요. 다음은 몇 가지 예시를 보여 줘요.
structure /appointment/ ! nested structure definition: app_time is an array of two 'time' structure /time/ app_time (2) integer(1) hour, minute end structure character(10) memo end structure ! The 'time' structure is still usable record /time/ now now = time(5, 30) ... structure /appointment/ ! anonymous nested structure definition structure start, end integer(1) hour, minute end structure character(10) memo end structure
- 구조체는
UNION블록을 포함할 수 있어요. 자세한 내용은UNIONandMAP절을 참고해요. - 구조체는 Old-style variable initialization에 설명된 것 같은 구식 구성 요소 초기화를 지원해요. 배열 초기화자의 경우,
<literal-integer> * <constant-initializer>형태의 반복 지정자를 포함할 수 있어요. 정수 값은 초기화자 목록을 확장할 때 상수 초기화자를 몇 번 반복할지를 나타내요.
더 알아보기
- Extensions implemented in GNU Fortran — gfortran이 구현한 확장 전체 목록
- UNION and MAP — 공용체·매핑 확장
- -fdec-structure — DEC 구조체 지원 플래그