MainframeMaster

COBOL Tutorial

COBOL SIZE Clause - Quick Reference

Progress0 of 0 lessons

Overview

The SIZE clause is used to control data item size and memory allocation in COBOL. It provides precise control over how much memory is allocated for data items, enabling optimization of memory usage and ensuring consistent storage allocation across different platforms and environments.

Purpose and Usage

  • Memory control - Control memory allocation for data items
  • Storage optimization - Optimize storage usage
  • Platform consistency - Ensure consistent sizes across platforms
  • Performance optimization - Optimize performance through proper sizing
  • Resource management - Manage system resources efficiently

Syntax

The SIZE clause is used in the DATA DIVISION for data size and memory allocation control.

Basic Syntax

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
* Basic SIZE syntax 01 data-name PIC data-picture SIZE IS size-specification. * Complete example with different size specifications IDENTIFICATION DIVISION. PROGRAM-ID. SIZE-EXAMPLE. DATA DIVISION. WORKING-STORAGE SECTION. * Character data with specific size 01 SMALL-TEXT PIC X(50) SIZE IS 50. * Numeric data with specific size 01 SMALL-NUMBER PIC 9(5) SIZE IS 5. * Large character data with size control 01 LARGE-TEXT PIC X(200) SIZE IS 200. * Numeric data with decimal places 01 DECIMAL-NUMBER PIC 9(7)V99 SIZE IS 9. PROCEDURE DIVISION. MAIN-LOGIC. * Assign values to sized data items MOVE "Hello World" TO SMALL-TEXT MOVE 12345 TO SMALL-NUMBER MOVE "This is a large text field with controlled size allocation" TO LARGE-TEXT MOVE 1234567.89 TO DECIMAL-NUMBER DISPLAY "Small text: " SMALL-TEXT DISPLAY "Small number: " SMALL-NUMBER DISPLAY "Large text: " LARGE-TEXT DISPLAY "Decimal number: " DECIMAL-NUMBER STOP RUN.

SIZE provides precise control over memory allocation and storage size for data items.

Practical Examples

Examples of using the SIZE clause in different scenarios with detailed explanations.

Database Application - Optimized Record Storage

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
* Database application using SIZE for optimized record storage DATA DIVISION. WORKING-STORAGE SECTION. * Customer record with controlled sizes 01 CUSTOMER-RECORD. 05 CUSTOMER-ID PIC 9(8) SIZE IS 8. 05 CUSTOMER-NAME PIC X(30) SIZE IS 30. 05 CUSTOMER-EMAIL PIC X(50) SIZE IS 50. 05 CUSTOMER-PHONE PIC X(15) SIZE IS 15. 05 CUSTOMER-BALANCE PIC 9(10)V99 SIZE IS 12. 05 CUSTOMER-STATUS PIC X(1) SIZE IS 1. * Product record with memory optimization 01 PRODUCT-RECORD. 05 PRODUCT-CODE PIC X(10) SIZE IS 10. 05 PRODUCT-NAME PIC X(40) SIZE IS 40. 05 PRODUCT-PRICE PIC 9(7)V99 SIZE IS 9. 05 PRODUCT-QUANTITY PIC 9(5) SIZE IS 5. 05 PRODUCT-CATEGORY PIC X(20) SIZE IS 20. PROCEDURE DIVISION. PROCESS-CUSTOMER-DATA. * Initialize customer record with controlled sizes MOVE 12345678 TO CUSTOMER-ID MOVE "John Doe" TO CUSTOMER-NAME MOVE "john.doe@email.com" TO CUSTOMER-EMAIL MOVE "(555) 123-4567" TO CUSTOMER-PHONE MOVE 1500.75 TO CUSTOMER-BALANCE MOVE "A" TO CUSTOMER-STATUS DISPLAY "Customer ID: " CUSTOMER-ID DISPLAY "Customer Name: " CUSTOMER-NAME DISPLAY "Customer Email: " CUSTOMER-EMAIL DISPLAY "Customer Phone: " CUSTOMER-PHONE DISPLAY "Customer Balance: " CUSTOMER-BALANCE DISPLAY "Customer Status: " CUSTOMER-STATUS * Initialize product record MOVE "PROD001" TO PRODUCT-CODE MOVE "Laptop Computer" TO PRODUCT-NAME MOVE 999.99 TO PRODUCT-PRICE MOVE 100 TO PRODUCT-QUANTITY MOVE "Electronics" TO PRODUCT-CATEGORY DISPLAY "Product Code: " PRODUCT-CODE DISPLAY "Product Name: " PRODUCT-NAME DISPLAY "Product Price: " PRODUCT-PRICE DISPLAY "Product Quantity: " PRODUCT-QUANTITY DISPLAY "Product Category: " PRODUCT-CATEGORY.

This example demonstrates how SIZE optimizes memory usage in database applications by controlling the exact storage size for each field in customer and product records. Each field has a precisely defined size that matches its expected data requirements, preventing memory waste while ensuring sufficient space for the data.

Embedded System - Memory-Constrained Environment

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
* Embedded system using SIZE for memory-constrained environment DATA DIVISION. WORKING-STORAGE SECTION. * Sensor data with minimal memory usage 01 SENSOR-DATA. 05 TEMPERATURE PIC S9(3) SIZE IS 3. 05 HUMIDITY PIC 9(3) SIZE IS 3. 05 PRESSURE PIC 9(4) SIZE IS 4. 05 STATUS-FLAG PIC X(1) SIZE IS 1. 05 ERROR-CODE PIC 9(2) SIZE IS 2. * Communication buffer with controlled size 01 COMM-BUFFER. 05 MESSAGE-ID PIC X(8) SIZE IS 8. 05 MESSAGE-TYPE PIC X(4) SIZE IS 4. 05 MESSAGE-DATA PIC X(32) SIZE IS 32. 05 CHECKSUM PIC X(2) SIZE IS 2. * System configuration with optimized storage 01 SYSTEM-CONFIG. 05 DEVICE-ID PIC X(6) SIZE IS 6. 05 VERSION-NUM PIC 9(2)V9(2) SIZE IS 4. 05 OPERATION-MODE PIC X(1) SIZE IS 1. 05 TIMEOUT-VALUE PIC 9(3) SIZE IS 3. PROCEDURE DIVISION. MANAGE-EMBEDDED-SYSTEM. * Initialize sensor data with minimal memory usage MOVE 25 TO TEMPERATURE MOVE 60 TO HUMIDITY MOVE 1013 TO PRESSURE MOVE "N" TO STATUS-FLAG MOVE 0 TO ERROR-CODE DISPLAY "Temperature: " TEMPERATURE "°C" DISPLAY "Humidity: " HUMIDITY "%" DISPLAY "Pressure: " PRESSURE " hPa" DISPLAY "Status: " STATUS-FLAG DISPLAY "Error Code: " ERROR-CODE * Initialize communication buffer MOVE "MSG001" TO MESSAGE-ID MOVE "DATA" TO MESSAGE-TYPE MOVE "Sensor reading data packet" TO MESSAGE-DATA MOVE "AB" TO CHECKSUM DISPLAY "Message ID: " MESSAGE-ID DISPLAY "Message Type: " MESSAGE-TYPE DISPLAY "Message Data: " MESSAGE-DATA DISPLAY "Checksum: " CHECKSUM * Initialize system configuration MOVE "DEV001" TO DEVICE-ID MOVE 1.25 TO VERSION-NUM MOVE "N" TO OPERATION-MODE MOVE 300 TO TIMEOUT-VALUE DISPLAY "Device ID: " DEVICE-ID DISPLAY "Version: " VERSION-NUM DISPLAY "Operation Mode: " OPERATION-MODE DISPLAY "Timeout: " TIMEOUT-VALUE " seconds".

This example shows how SIZE optimizes memory usage in embedded systems where memory is severely constrained. Each data structure (sensor data, communication buffer, system configuration) uses precisely sized fields that minimize memory consumption while providing sufficient space for the required data. This is crucial for embedded systems where every byte of memory counts.

Financial Application - Precise Amount Storage

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
* Financial application using SIZE for precise amount storage DATA DIVISION. WORKING-STORAGE SECTION. * Transaction record with controlled sizes 01 TRANSACTION-RECORD. 05 TRANSACTION-ID PIC 9(12) SIZE IS 12. 05 ACCOUNT-NUMBER PIC 9(10) SIZE IS 10. 05 TRANSACTION-TYPE PIC X(4) SIZE IS 4. 05 TRANSACTION-AMOUNT PIC 9(10)V99 SIZE IS 12. 05 TRANSACTION-DATE PIC 9(8) SIZE IS 8. 05 TRANSACTION-TIME PIC 9(6) SIZE IS 6. 05 STATUS-CODE PIC X(2) SIZE IS 2. * Currency conversion with precise sizing 01 CURRENCY-CONVERSION. 05 SOURCE-CURRENCY PIC X(3) SIZE IS 3. 05 TARGET-CURRENCY PIC X(3) SIZE IS 3. 05 EXCHANGE-RATE PIC 9(6)V6 SIZE IS 12. 05 CONVERSION-AMOUNT PIC 9(10)V99 SIZE IS 12. 05 CONVERSION-FEE PIC 9(5)V99 SIZE IS 7. * Account summary with optimized storage 01 ACCOUNT-SUMMARY. 05 ACCOUNT-ID PIC 9(10) SIZE IS 10. 05 CURRENT-BALANCE PIC S9(12)V99 SIZE IS 14. 05 AVAILABLE-BALANCE PIC S9(12)V99 SIZE IS 14. 05 HOLD-AMOUNT PIC 9(10)V99 SIZE IS 12. 05 LAST-TRANSACTION PIC 9(8) SIZE IS 8. PROCEDURE DIVISION. PROCESS-FINANCIAL-TRANSACTIONS. * Initialize transaction record MOVE 123456789012 TO TRANSACTION-ID MOVE 9876543210 TO ACCOUNT-NUMBER MOVE "DEBT" TO TRANSACTION-TYPE MOVE 1500.75 TO TRANSACTION-AMOUNT MOVE 20231215 TO TRANSACTION-DATE MOVE 143022 TO TRANSACTION-TIME MOVE "OK" TO STATUS-CODE DISPLAY "Transaction ID: " TRANSACTION-ID DISPLAY "Account Number: " ACCOUNT-NUMBER DISPLAY "Transaction Type: " TRANSACTION-TYPE DISPLAY "Amount: " TRANSACTION-AMOUNT DISPLAY "Date: " TRANSACTION-DATE DISPLAY "Time: " TRANSACTION-TIME DISPLAY "Status: " STATUS-CODE * Initialize currency conversion MOVE "USD" TO SOURCE-CURRENCY MOVE "EUR" TO TARGET-CURRENCY MOVE 0.850000 TO EXCHANGE-RATE MOVE 1000.00 TO CONVERSION-AMOUNT MOVE 5.50 TO CONVERSION-FEE DISPLAY "Source Currency: " SOURCE-CURRENCY DISPLAY "Target Currency: " TARGET-CURRENCY DISPLAY "Exchange Rate: " EXCHANGE-RATE DISPLAY "Conversion Amount: " CONVERSION-AMOUNT DISPLAY "Conversion Fee: " CONVERSION-FEE * Initialize account summary MOVE 1234567890 TO ACCOUNT-ID MOVE 50000.00 TO CURRENT-BALANCE MOVE 45000.00 TO AVAILABLE-BALANCE MOVE 5000.00 TO HOLD-AMOUNT MOVE 20231215 TO LAST-TRANSACTION DISPLAY "Account ID: " ACCOUNT-ID DISPLAY "Current Balance: " CURRENT-BALANCE DISPLAY "Available Balance: " AVAILABLE-BALANCE DISPLAY "Hold Amount: " HOLD-AMOUNT DISPLAY "Last Transaction: " LAST-TRANSACTION.

This example demonstrates how SIZE ensures precise storage for financial data where accuracy and consistency are critical. Each field in transaction records, currency conversions, and account summaries has a precisely defined size that accommodates the expected data range while preventing memory waste. This is especially important in financial applications where data integrity and storage efficiency are paramount.

Best Practices

Understanding best practices ensures effective use of the SIZE clause.

Best Practices

  • Choose appropriate sizes - Select sizes based on expected data requirements
  • Consider memory constraints - Balance size with memory availability
  • Validate data fits - Ensure data fits within specified sizes
  • Document size requirements - Document why specific sizes were chosen
  • Test boundary conditions - Test with maximum expected data sizes

Test Your Knowledge

1. What is the primary purpose of the SIZE clause in COBOL?

  • To resize files
  • To control data item size and memory allocation
  • To resize programs
  • To resize memory

2. In which COBOL division is the SIZE clause typically used?

  • IDENTIFICATION DIVISION
  • ENVIRONMENT DIVISION
  • DATA DIVISION
  • PROCEDURE DIVISION

3. What does the SIZE clause control in data definition?

  • Data validation
  • Memory allocation and storage size
  • Data type
  • Data format

4. Can SIZE be used with all data types?

  • Yes, with all data types
  • No, only with numeric types
  • No, only with character types
  • Only with specific data types

5. What is the relationship between SIZE and memory usage?

  • They are unrelated
  • SIZE directly controls memory allocation
  • SIZE only affects display
  • SIZE is for validation only

Frequently Asked Questions