Menu ▾ ▴

#106 Let ALLOCATE set the length of the target item

invalid
wont-fix
None
5 - default
2016-02-20
2016-02-11
wim niemans
No
01 variableLengthItem any length pic x.

allocate 50 charaters returning myPointer.
set address of variableLengthItem to myPointer.

Above construct still makes only available the first byte, since the length is not adjusted in the cobol field.
According to MF docs, even a move should adjust the length:

move "abcde" to variableLengthItem.

which should adjust the length of the item to 5.

Why needed?
My system has variable text, say dutch, english and german variations. A function will retrieve the text, which has variable length of course. The result must be unstringed and restringed again to obtain normalised displaying of these text. The unstring (and re-string) should be totally obsolete.

Related

Wish List: #109

Discussion

  • Edward Hart

    Edward Hart - 2016-02-11

    Hi Wim,

    the ANY LENGTH clause does not imply that an item's size may be varied within the program/function: the length of an ANY LENGTH is item is the same as the corresponding argument passed to the program/function.

    The feature you're requesting is DYNAMIC LENGTH, which was added in COBOL 2014 and allows for the more flexible string-handling found in modern languages.

     
    • wim niemans

      wim niemans - 2016-02-18

      I see. Didn't know about DYNAMIC LENGTH.
      This does mean that creating a variable length string in a function is not possible until DYNAMIC LENGTH is available, I guess? Of course using odo is alternative, though not really elegant.

       
      • Edward Hart

        Edward Hart - 2016-02-18

        This does mean that creating a variable length string in a function is not possible until DYNAMIC LENGTH is available, I guess?

        Correct.

        Is there anything stopping you from using a PIC X(500), say, and trimming stuff as necessary? It'll be no different from an ODO which is also of fixed size (despite pretending not to be).

         
        • wim niemans

          wim niemans - 2016-02-18

          Is there anything stopping you from using a PIC X(500)

          Yes. There is no real limitation: the variable can be length zero, can be any length.
          Than, working with x(500) or, say, x(21634) is ugly and breathes amateurism.
          The only choice is wait for it, leave development or use a c routine emulating a function.
          This may fire back, I know.

           
        • Simon Sobisch

          Simon Sobisch - 2016-02-18

          One of the nice parts about alphanumeric "string" functions is the variable length.
          GC should allow setting the length for ANY LENGTH returning items in UDF, but I have not seen a clear documentation (including in the standard) how to set this.
          This looks like a good reason for Brian to ask his comitee contacts.

           
          • Edward Hart

            Edward Hart - 2016-02-19

            GC should allow setting the length for ANY LENGTH returning items in UDF, but I have not seen a clear documentation (including in the standard) how to set this.

            There's no documentation because the standard doesn't allow it:

            [an ANY LENGTH item is] treated as though there were n repetitions of the picture symbol in the character-string in its PICTURE clause, where n is the length of the corresponding argument or returning item of the activating runtime element.

            The length of this PICTURE clause is fixed while in the function, so the length of the ANY LENGTH item cannot be varied from within the function. Only DYNAMIC LENGTH lets you freely vary the length of item.

             
            • wim niemans

              wim niemans - 2016-02-19

              The OO variant of MF defines a PROPERTY clause with SET and GET
              PROPERTY clause
              and a SET syntax
              SET satement

              SET SIZE OF *identifier* [ TO | = ] [ *identifier* | *integer* ]
              

              and a syntax for RETURNING in the PROCEDURE DIVISION header, see Format-3 in PROCEDURE HEADER

              RETURNING *data-name* AS *type-name* [ *attribute clause* ]
              

              Well, these options come from various compilers, and I'm not sure if it can be found in the standard. Now on GC:

              The ANY LENGTH clause is intended for defining USING paramaters and the item will inherit the length of the caller's item. Though it is possible to define such item as a RETURNING item without any reference to a USING item; at least the compiler has no problems. GC allocates for this ittem 1 byte by cob_malloc(1U), which is arbitrary (a zero length would just do). This state-of-the-art is pretty useless and a simple realloc on the very first reference would do the job. It is impossible to adjust the length of such item using a called c-subroutine; that MUST be a emulated function. This raises the problem that the item with adjusted length cannot be stored, since the function returns a temporary item.

              Having said all this, one should realize the Cobol is about fixed length data; so a solution for returning variable length items interferes with Cobol's architecture. OO variants however implemented it easily.

               

              Last edit: wim niemans 2016-02-19
              • Edward Hart

                Edward Hart - 2016-02-19

                That SET statement and RETURNING ... AS ... is part of MF's Managed COBOL dialect, which appears to be a blatant attempt at vendor lock-in.

                GC allocates for this ittem 1 byte by cob_malloc(1U), which is arbitrary (a zero length would just do).

                I'm not sure what you're referring to here.

                 
                • wim niemans

                  wim niemans - 2016-02-19

                  GC allocates for this ittem 1 byte by cob_malloc(1U), which is arbitrary (a zero length would just do).

                  I'm not sure what you're referring to here.

                  The udf:

                  linkage section.
                  01      stringx any length pic x.
                  01      stringy any length pic x.
                  procedure division using stringx returning stringy.
                  

                  The result is that stringy has a size of 1. It is impossible to change that size.

                   

                  Last edit: wim niemans 2016-02-19
              • Simon Sobisch

                Simon Sobisch - 2016-02-19

                The docs you've referred to neither apply to a real COBOL environment but to MF'S .NET/JVM compilers (and therefore does not even count as "make migration easier"), nor to ANY LENGTH items.

                It's very unlikely that GnuCOBOL will support these as an extension.

                 
            • Simon Sobisch

              Simon Sobisch - 2016-02-19

              Hm, some additional quotes from the standard:

              Data-name-2 [the data name in the RETURNING phrase] is the name used in the function [...] or program for the result that is returned to the activating element according to 14.6.5, Results of runtime element execution.
              NOTE In COBOL, the storage for the returning item is allocated in the activating source unit. The activated element contains only a formal description in its linkage section.

              and

              14.6.5 Results of runtime element execution
              The result of the execution of a [...] function [...] that specifies a RETURNING phrase in its procedure division header, is the content of the data item referenced by that RETURNING phrase.
              The result becomes available to the activating element after the activated element returns as follows:
              [...] If the runtime element is activated by a function-identifier [...], the result is placed in the temporary data item referenced by that identifier.

              And now the main part:

              13.18.2 ANY LENGTH clause
              The ANY LENGTH clause specifies that the length or a linkage section item may vary at runtime and is determined by the length of the argument in the calling program, [...] or function

              The ANY LENGTH clause specifies that the subject of the entry shall be:
              a) a zero-length item when the corresponding argument or returning item of the activating runtime element is a zero-length item, or
              b) treated as though there were n repetitions of the picture symbol in the character-string in its PICTURE clause, where n is the length of the corresponding argument or returning item of the activating runtime element.

              Conclusion (maybe confusion):
              If a function RETURNING an ANY LENGTH item is used, it must have an returning item. Therefore it's not useable in STRING, DISPLAY or other statements like this as these don't have a returning item. The only possible statement I can think of is MOVE function..., maybe WRITE something FROM function....

              In this case a MOVE would set the size for the ANY LENGTH item, but in the calling program.

              The runtime would have to check for this and set the length accordingly. For example:

              ::cobolfree
                 FUNCTION-ID. testfunc.
                 DATA DIVISION.
                 LINKAGE SECTION.
                 01 var-inp ANY LENGTH.
                 01 var-out ANY LENGTH.
                 PROCEDURE DIVISION USING var-inp RETURNING var-out.
                    move var-inp to var-out
                    inspect var-out converting 'abcdefghi' to '123456789'
                    EXIT FUNCTION.
                 END FUNCTION testfunc.
              
                 PROGRAM-ID. testprog.
                 ENVIRONMENT DIVISION.
                 CONFIGURATION SECTION.
                 REPOSITORY.
                    FUNCTION testfunc.
                 DATA DIVISION.
                 WORKING-STORAGE SECTION.
                 01 var1   PIC X(15).
                 01 var2   PIC X(21).
                 PROCEDURE DIVISION.
                    MOVE 'a test, this is' to var1
                    MOVE testfunc (var1)  to var2
                    display '"' var1 '" - "' var2 '"' end-display
                    MOVE 'another test, this is' to var2
                    MOVE testfunc (var2)  to var1
                    display '"' var2 '" - "' var1 '"' end-display
                    STOP RUN.
                 END PROGRAM testprog.
              

              The expected result from the quoted specs would be:

              "a test, this is" - "1 t5st, th9s 9s      "
              "another test, this is" - "1noth5r t5st, t"
              

              but the result is:

              "a test, this is" - "1                    "
              "another test, this is" - "1              "
              

              as the returning item is allocated with one byte and never changed.

              Is this a valid conclusion or only a confusion? Any thoughts or maybe even knowledge?

              Simon

              BTW: I'd still like to have a statement from the standard comitee.

               

              Last edit: Simon Sobisch 2016-02-19
              • Edward Hart

                Edward Hart - 2016-02-20

                If a function RETURNING an ANY LENGTH item is used, it must have an returning item.

                Functions aren't allowed to return ANY LENGTH items (§ 13.8.2.2.4):

                If the source element containing the ANY LENGTH clause is a function, the subject of the entry shall be ... a formal parameter with the BY REFERENCE phrase.

                 

                Last edit: Edward Hart 2016-02-20
                • wim niemans

                  wim niemans - 2016-02-20

                  I don't know from where you're quoting this, and I am not aware of this limitation.
                  Besides that the compiler gladly accepts the construct.

                   
                  • Edward Hart

                    Edward Hart - 2016-02-20

                    I don't know from where you're quoting this, and I am not aware of this limitation.

                    I'm also quoting from the 2014 COBOL standard.

                    Besides that the compiler gladly accepts the construct.

                    The compiler is wrong ;-). I'll start fixing this now.

                     
                    • Simon Sobisch

                      Simon Sobisch - 2016-02-20

                      This is true, I was confused by the part "ANY LENGTH may be used [...] as RETURNING item" in the standard, but this only applies to methods (which aren't available in GnuCOBOL).

                      Ed tracked and fixed the compiler error with [bugs:#203], therefore I'll close this ticket.

                       

                      Related

                      Bugs: #203

              • wim niemans

                wim niemans - 2016-02-20

                Yes, this is exactly what I figured out. There are 2 aspects:

                1. the RETURNING item with ANY LENGTH is set to have a length of 1, which should be 0 (zero)
                2. there is no means to set the desired length of that RETURNING item.

                Which means that functions written in Cobol cannot return variable length items, which makes it impossible to use string manipulation where the string length is varying (expanding string), like SUBSTITUTE '{tplVar}' IM stringx WITH 'John Doe, Washington'.

                My conclusion is that this can only be implemented using intrinsic functions or system supplied subroutines, since concatenate(A, B) does the job marvellous.

                 
  • Simon Sobisch

    Simon Sobisch - 2016-02-11

    I likely would have answered the same as Ed. But with a question added:

    According to MF docs, even a move should adjust the length:

    Pleaes link the actual doc you refer to and we see what implementation is possible. Thank you.
    Simon

     
    • wim niemans

      wim niemans - 2016-02-18

      I am very sure I've read about that. The specific doc however cannot be found again; so leave it as not being written. Sorry.

       
      • wim niemans

        wim niemans - 2016-02-20

        Found it again, though it was NOT at MF, but from a discussion at ISO/IEC/JTC 1/SC 22/WG 4 in 2008. I must admit that I don't know about the status of this change (if it reached the standard or was redesigned as DYNAMIC LENGTH or was simply ignored).

        C.2.7 Any-length elementary items
        A truly any-length elementary item of unlimited size can be defined by coding the following picture format:

        05 notes PIC X ANY LENGTH.

        If the item has a maximum length (n), ANY LENGTH LIMIT n is written or, if there is no theoretical maximum length, ANY LENGTH is sufficient.

        Any-length items may be national items, or bit items:

        05 city-name PIC 1 ANY LENGTH.
        05 customer-name PIC N ANY LENGTH.

        When a new value is stored in an any-length elementary item, the item's length is automatically adjusted, subject to any maximum length. For example,

        MOVE "This product is no longer available" TO notes

        sets the length of notes to 35. To find this length, you use

        FUNCTION LENGTH (notes)

        Until its value changes again, this data item will now behave exactly like a fixed-length item with a PICTURE of X(35), and all the statements normally available to an alphanumeric or national data item, such as MOVE, STRING, UNSTRING, INSPECT, ACCEPT, DISPLAY can be used with it.

        Either of the following two statements

        INITIALIZE notes or MOVE SPACES TO notes

        reduces the item to a zero length data item.

         

        Last edit: wim niemans 2016-02-20
        • Simon Sobisch

          Simon Sobisch - 2016-02-20

          Thank you for the reference. I did not found this discussion but I have not checked everything at http://www.cobolstandard.info/wg4/document.html - maybe it's in there.

          It was good to take the time to research it: it was in before and there are different references (most for "upcoming 2008 standard") for this syntax available.

          Ed was correct again. The "dynamic" ANY LENGTH is included in the 2014 standard but as DYNAMIC LENGTH. For more information and the new ticket see [feature-requests:#109].

          It's funny to see that there are even books out (in this case written in German) declaring the old version with the "dynamic" ANY LENGTH to be part of the COBOL 2014 standard. But this book says that "COBOL has full XML support now, as it includes ISO/IEC TR 24716: 2007 completely", too...

           

          Related

          Wish List: #109

  • Simon Sobisch

    Simon Sobisch - 2016-02-20
    • status: open --> wont-fix
    • assigned_to: Edward Hart
    • Group: unclassified --> invalid
     
  • Simon Sobisch

    Simon Sobisch - 2016-02-20

    Thank you for reporting the compiler bug ;-)
    ANY LENGTH as returning item should not be possible at all but marked as an error. Therefore I'm closing this ticket as invalid.

    BTW: The compiler bug was fixed by Ed in [r775].

     
    • wim niemans

      wim niemans - 2016-05-24

      Please note the following statement from ISO/IEC 1989:200x WD 1.10, on 8.5.1.7 Any-length elementary items

      If an any-length elementary item is a receiving operand and is not reference-modified, 
      the new value becomes the content of the item. 
      The new length of the any-length elementary item is determined by the length of new content.
      

      There is nothing in the document that prohibits a returng item of ANY LENGTH. But okay, it is not the last document on 2014 standard.

       

Log in to post a comment.