When you are building a 64-bit application, your data structure must be aligned on an 8-byte boundary, that is, to memory addresses that are multiples of 8 bytes. Similarly, the data structure of 32-bit applications must be aligned on a 4-byte boundary.
The following example illustrates this concept by creating a 32-bit and a 64-bit ESQL/COBOL version of the SQLDA, which is a descriptor area that describes objects that are referenced in dynamic SQL. The Sybase version of the SQLDA, written in C, is given as a reference in the following example.
This code snippet shows the SQLDA layout supplied by Sybase:
typedef struct _sqlda { CS_SMALLINT sd_sqln; CS_SMALLINT sd_sqld; struct _sd_column { CS_DATAFMT sd_datafmt; CS_VOID *sd_sqldata; CS_SMALLINT sd_sqlind; CS_INT sd_sqllen; CS_VOID *sd_sqlmore; } sd_column[1];} syb_sqlda;typedef syb_sqlda SQLDA;
The following SQLDA structure shows the 32-bit ESQL/COBOL version of the Sybase-specific SQLDA:
01 OUT-DES. /* 32bit */ 09 SD-SQLN PIC S9(4) COMP. 09 SD-SQLD PIC S9(4) COMP. 09 SD-COLUMN OCCURS 27 TIMES. /* 27-column table*/ 19 SD-DATAFMT. 29 SQL--NM PIC X(256). 29 SQL--NMLEN PIC S9(9) COMP. 29 SQL--DATATYPE PIC S9(9) COMP. 29 SQL--FORMAT PIC S9(9) COMP. 29 SQL--MAXLENGTH PIC S9(9) COMP. 29 SQL--SCALE PIC S9(9) COMP. 29 SQL--PRECISION PIC S9(9) COMP. 29 SQL--STTUS PIC S9(9) COMP. 29 SQL--COUNT PIC S9(9) COMP. 29 SQL--USERTYPE PIC S9(9) COMP. 29 SQL--LOCALE PIC S9(9) COMP. 19 SD-SQLDATA PIC S9(9) COMP. 19 SD-SQLIND PIC S9(4) COMP. 19 FILLER PIC S9(4) COMP. /* Filler record to */ /* align SQLIND */ 19 SD-SQLLEN PIC S9(9) COMP. 19 SD-SQLMORE PIC S9(9) COMP.
In the 32-bit ESQL/COBOL version of the Sybase-specific SQLDA, the picture (PIC) clauses of relevance are:
Elements defined as S9(4) – S9(4) is the ESQL/COBOL equivalent of smallint, which is 2 bytes in length. On its own, an element defined as S9(4) does not meet the 32-bit data alignment requirement. However, an S9(4) pair, as in the case of SD-SQLN and SD-SQLD, meets this requirement because, together, the elements occupy a memory address that is a multiple of 4 bytes.
Elements defined as S9(9) – S9(9) is the ESQL/COBOL equivalent of an int, which is 4 bytes in length. Elements defined as S9(9) meet the 32-bit data alignment requirement.
FILLER – a filler record 2 bytes in length is added to pad SD-SQLIND, which is an unpaired S9(4) element, and to align the entire structure on a 4-byte boundary.
The following SQLDA structure shows the 64-bit ESQL/COBOL version of the Sybase-specific SQLDA. In a 64-bit environment, the entire data structure must align on an 8-byte boundary:
01 OUT-DES. /* 64 bit */ 09 SD-SQLN PIC S9(4) COMP. 09 SD-SQLD PIC S9(4) COMP. 09 FILLER PIC S9(9) COMP. /* First filler to align */ /* on eight bytes */ 09 SD-COLUMN OCCURS 27 TIMES. /* 27-column table */ 19 SD-DATAFMT. 29 SQL--NM PIC X(256). 29 SQL--NMLEN PIC S9(9) COMP. 29 SQL--DATATYPE PIC S9(9) COMP. 29 SQL--FORMAT PIC S9(9) COMP. 29 SQL--MAXLENGTH PIC S9(9) COMP. 29 SQL--SCALE PIC S9(9) COMP. 29 SQL--PRECISION PIC S9(9) COMP. 29 SQL--STTUS PIC S9(9) COMP. 29 SQL--COUNT PIC S9(9) COMP. 29 SQL--USERTYPE PIC S9(9) COMP. 29 FILLER PIC S9(9) COMP. /* Second filler */ 29 SQL--LOCALE PIC S9(18) COMP. /* locale is */ /* now eight bytes */ 19 SD-SQLDATA PIC S9(18) COMP. /* SQLDATA is */ /* now eight bytes */ 19 SD-SQLIND PIC S9(4) COMP. 19 FILLER PIC S9(4) COMP. /* Third filler */ 19 SD-SQLLEN PIC S9(9) COMP. 19 SD-SQLMORE PIC S9(18) COMP. /* SQLMORE is */ /* now eight bytes */
In the 64-bit ESQL/COBOL version of the SQLDA, the PIC clauses of relevance are:
Elements defined as S9(4) – S9(4) is equivalent to an ESQL/COBOL smallint, which is 2 bytes in length. On its own, an element defined as S9(4) does not meet the 64-bit requirement because the 64-bit architecture requires that memory addresses be in multiples of 8. To meet the requirement, an S9(4) element must be grouped with other elements or padded using a filler. In the 64-bit version of the SQLDA above, the combined length of SD-SQLN and SD-SQLD is only 4 bytes, thus, a filler 4 bytes in length is added after SD-SQLD.
Elements defined as S9(9) – S9(9) is equivalent to an ESQL/COBOL int, which is 4 bytes in length. A pair of S9(9) elements, such as SQL-NMELEN and SQL-DATATYPE, meets the 64-bit alignment requirement.
Elements defined as S9(18) – S9(18) is the ESQL/COBOL equivalent of a pointer or long, which is 8 bytes in length. Elements defined as S9(18) meet the 64-bit data alignment requirement.
FILLER – in the above example, three fillers of varying lengths are used to pad and align the data structure on an 8-byte boundary.
Although you can use fillers to pad and align the SQLDA data
structure, do not modify the SQLDA data structure. You cannot add
or delete an SQLDA element, or edit the element’s current
definition.