Data alignment optimizes memory layout for performance and hardware compatibility. Learn to design efficient data structures, minimize padding, and optimize memory usage in COBOL programs.
123456789WORKING-STORAGE SECTION. 01 CUSTOMER-RECORD. 05 CUSTOMER-ID PIC 9(6). 05 CUSTOMER-NAME PIC X(30). 05 CUSTOMER-BALANCE PIC 9(9)V99. 05 CUSTOMER-STATUS PIC X. 05 CUSTOMER-DATE PIC 9(8). *> Total size: 6 + 30 + 11 + 1 + 8 = 56 bytes
COBOL automatically aligns data fields in the order they are declared. Each field occupies its declared size in memory. Consider field ordering to minimize padding and optimize access patterns.
12345678910111213WORKING-STORAGE SECTION. 01 ALIGNED-RECORD. 05 RECORD-ID PIC 9(4). *> 4 bytes 05 FILLER PIC X(4). *> Padding to 8-byte boundary 05 AMOUNT PIC 9(9)V99. *> 11 bytes 05 FILLER PIC X(5). *> Padding to 16-byte boundary 05 TIMESTAMP PIC 9(8). *> 8 bytes *> Alternative: Use COMP fields for better alignment 01 COMP-ALIGNED-RECORD. 05 RECORD-ID PIC 9(4) COMP. *> 4 bytes, aligned 05 AMOUNT PIC 9(9)V99 COMP-3. *> 6 bytes, packed decimal 05 TIMESTAMP PIC 9(8) COMP. *> 8 bytes, aligned
Align data fields to hardware boundaries (typically 4, 8, or 16 bytes) for optimal performance. Use FILLER fields for padding or COMP fields for automatic alignment. COMP fields are more efficient for numeric data.
12345678910111213141516WORKING-STORAGE SECTION. 01 EFFICIENT-GROUP. 05 GROUP-HEADER. 10 GROUP-ID PIC 9(4) COMP. 10 GROUP-TYPE PIC X. 10 FILLER PIC X(3). *> Align to 8-byte boundary 05 GROUP-DATA. 10 DATA-COUNT PIC 9(4) COMP. 10 DATA-SIZE PIC 9(4) COMP. 10 DATA-VALUE PIC X(20). *> Poor alignment example 01 INEFFICIENT-GROUP. 05 SMALL-FIELD PIC X. 05 LARGE-FIELD PIC X(100). 05 ANOTHER-SMALL PIC X.
Group related fields together and align groups as units. Place frequently accessed fields together and align them to boundaries. Avoid mixing small and large fields which can cause inefficient memory usage.
1234567891011WORKING-STORAGE SECTION. 01 ALIGNED-ARRAY. 05 ARRAY-COUNT PIC 9(4) COMP. 05 FILLER PIC X(4). *> Align to 8-byte boundary 05 DATA-ARRAY OCCURS 100 TIMES. 10 ARRAY-ID PIC 9(4) COMP. 10 ARRAY-VALUE PIC 9(9)V99 COMP-3. 10 ARRAY-FLAG PIC X. 10 FILLER PIC X(3). *> Align each element *> Calculate total size: 8 + (100 * 16) = 1608 bytes
Align array elements to boundaries for efficient access. Use COMP fields for numeric array elements and add padding to align each element. This improves performance when accessing array elements sequentially.
1234567891011121314151617WORKING-STORAGE SECTION. 01 PERFORMANCE-OPTIMIZED. 05 FREQUENTLY-USED. 10 CUSTOMER-ID PIC 9(6) COMP. 10 STATUS-FLAG PIC X. 10 FILLER PIC X(1). *> Align to 8-byte boundary 05 LESS-FREQUENT. 10 CUSTOMER-NAME PIC X(30). 10 CUSTOMER-ADDRESS PIC X(50). 10 CUSTOMER-PHONE PIC X(15). *> Access pattern optimization PROCESS-CUSTOMER. IF STATUS-FLAG = 'A' *> Frequently checked field PERFORM PROCESS-ACTIVE-CUSTOMER END-IF DISPLAY CUSTOMER-NAME *> Less frequent access
Place frequently accessed fields together and align them for optimal performance. Group fields by access frequency and align them to boundaries. This reduces memory access time and improves cache efficiency.
1234567891011121314WORKING-STORAGE SECTION. 01 MEMORY-EFFICIENT-LAYOUT. 05 HEADER-SECTION. 10 RECORD-TYPE PIC X. 10 RECORD-SIZE PIC 9(4) COMP. 10 FILLER PIC X(3). *> Align to 8-byte boundary 05 DATA-SECTION. 10 PRIMARY-DATA. 15 ID-FIELD PIC 9(6) COMP. 15 STATUS-FIELD PIC X. 15 FILLER PIC X(1). *> Align to 8-byte boundary 10 SECONDARY-DATA. 15 NAME-FIELD PIC X(20). 15 DESCRIPTION PIC X(40).
Design memory layout considering access patterns, field sizes, and alignment requirements. Group related data together, align frequently accessed fields, and minimize padding through careful field ordering.
12345678910111213WORKING-STORAGE SECTION. 01 PORTABLE-RECORD. 05 RECORD-LENGTH PIC 9(4) COMP. 05 RECORD-VERSION PIC 9(2) COMP. 05 FILLER PIC X(4). *> Ensure 8-byte alignment 05 RECORD-DATA PIC X(100). *> Use standard sizes for portability 01 STANDARD-SIZES. 05 INT-32 PIC S9(9) COMP. *> 32-bit integer 05 INT-64 PIC S9(18) COMP. *> 64-bit integer 05 FLOAT-32 PIC S9(7)V9(7) COMP-1. *> 32-bit float 05 FLOAT-64 PIC S9(15)V9(15) COMP-2. *> 64-bit float
Design data structures for cross-platform compatibility. Use standard data sizes and alignments that work across different hardware architectures. Consider endianness and alignment requirements for data exchange.
1234567891011121314WORKING-STORAGE SECTION. 01 BEST-PRACTICES-RECORD. 05 CRITICAL-FIELDS. 10 ID PIC 9(6) COMP. 10 STATUS PIC X. 10 FILLER PIC X(1). *> Align to 8-byte boundary 05 FREQUENT-FIELDS. 10 AMOUNT PIC 9(9)V99 COMP-3. 10 DATE-FIELD PIC 9(8) COMP. 05 INFREQUENT-FIELDS. 10 DESCRIPTION PIC X(50). 10 NOTES PIC X(100). *> Access pattern: Critical -> Frequent -> Infrequent
Follow alignment best practices: group fields by access frequency, align critical fields to boundaries, use COMP fields for numeric data, and minimize padding through careful field ordering. Document alignment decisions for maintenance.