Data alignment on a 64-bit architecture

When building a 64-bit application, your data structure must be aligned on an eight-byte boundary, that is, to memory addresses that are multiples of eight bytes. Similarly, the data structure of 32-bit applications must be aligned on a four-byte boundary.

The following example illustrates this concept by creating a 32-bit and 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.

Example

Example 1

Sybase version of the SQLDA

This code snippet shows the SQLDA layout that is 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;

32-bit ESQL/COBOL version of the Sybase-specific 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 given above, the picture (PIC) clauses of relevance are:

64-bit ESQL/COBOL version of the Sybase-specific SQLDA

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 eight-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 given above, the PIC clauses of relevance are:

NoteAlthough 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.