Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to declare variable length data-item

Status
Not open for further replies.

solanki123

Programmer
Jul 1, 2003
29
IN
Can anybody tell me that how to declare variable length data-item in cobol?
I mean if I do not know how much characters will be stored in that data-item but i know the range(eg. 10 to 100 characters), in that case how should I declare that data-item and how much memory will be occupied?
 
Code:
01  ITEM-LENGTH PIC 9(02).
01  VARIABLE-DATA-ITEM.
    05  FILLER PIC X(01) OCCURS 20 TO 100 TIMES DEPENDING ON ITEM-LENGTH.
 
Hi

The best method I can think of is also a bit complex ...

Code:
working-storage section.
01 var-length pic 9(05).

linkage section.
01 my-var pic x(100).

1. Call your compiler's function for allocating memory and give it the number of bytes you need to allocate.
2. Set the address of my-var to the address returned by the function.

Personally, I use ACU Cobol, so I would use this:

Code:
procedure division.
main-paragraph.
move 50 to var-length
call "M$ALLOC" using var-length, address of my-var
move all "*" to my-var(1:var-length)
display my-var(1:var-length)
call "M$FREE" using my-var

The DISPLAY command will display 50 stars. It's imrpotant to note that you MUST free the memory when you're doing with it (although the runtime would probably do it for when it shuts down, but still ...).
Also, the size of my_var must have an upper limit defined inthe linkage section (in this case 100) even though you may never reach that size. Just because you're defining it as x(100), though, doesn't mean it takes 100 bytes. It'll only take what you specified in the call to M$ALLOC.

Last but not least - make sure you never do anything in the unallocated area of my_var (i.e. my_var(51:) ). The results could be a little unpredictable as this is unassigned memory.


Hope that helps [smile]
.DaviD.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top