Home > Archive > Cobol > October 2006 > (Sort-of OT) - ANY LENGTH data items
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
(Sort-of OT) - ANY LENGTH data items
|
|
| William M. Klein 2006-10-12, 6:55 pm |
| In the Standards work on the next (2008 or so) COBOL Standard, they have
introduced a NEW "elementary any length data item". (The '02 Standard has ANY
LENGTH 01-levels).
There are two types of ANYLENGTH items, one includes the data "inline" (DIRECT)
and the other has it stored elsewhere (INDIRECT). To me, the DIRECT format has
some POTENTIAL problems and (fairly closely) resembles those existing COBOL
implementations with an EXTENSION that allows for nested ODO (OCCURS DEPENDING
ON) or data after an ODO.
My OFT-TOPIC question to this group is whether any of you can answer the
question of what OTHER languages (particularly C/C++ and Java) do as far as
"structures" go.
Do they allow "null-terminated" strings in the middle of "structures"?
Do they allow them as ARRAY (table) elements?
What about those that have "prefixed" (half-word or other binary "length)
fields - similar to SQL VARCHAR) in these places?
--
Bill Klein
wmklein <at> ix.netcom.com
| |
| Frank Swarbrick 2006-10-12, 6:55 pm |
| William M. Klein<wmklein@nospam.netcom.com> 10/10/06 5:25 PM >>>
>In the Standards work on the next (2008 or so) COBOL Standard, they have
>introduced a NEW "elementary any length data item". (The '02 Standard has
ANY
>LENGTH 01-levels).
>
>There are two types of ANYLENGTH items, one includes the data "inline"
(DIRECT)
>and the other has it stored elsewhere (INDIRECT). To me, the DIRECT format
has
>some POTENTIAL problems and (fairly closely) resembles those existing COBOL
>implementations with an EXTENSION that allows for nested ODO (OCCURS
DEPENDING
>ON) or data after an ODO.
>
>My OFT-TOPIC question to this group is whether any of you can answer the
>question of what OTHER languages (particularly C/C++ and Java) do as far as
>"structures" go.
>
>Do they allow "null-terminated" strings in the middle of "structures"?
For C I believe it's generally a pointer to a string that's in the
structure. For instance, here's probably a very poorly written C
program...
/* this defines what a structure looks like but does not allocate any
storage for it */
struct my_struct {
int fielda;
char fieldb;
char *fieldc;
float fieldd;
};
/* this allocates 33 bytes of storage (including the terminating 'null')
*/
char my_string[] = "This is a null terminated string";
void print_ms (struct my_struct *); /* function prototype */
void main () {
/* this actually allocates the storage; on the stack, in
this case (I think!) */
struct my_struct ms;
ms.fielda = 1;
ms.fieldb = 'a';
ms.fieldc = my_string;
ms.fieldd = 3.14;
printf ("length of ms = %d\n", sizeof ms);
/* call print_my_struct (), passing ms by reference */
print_my_struct (&ms);
}
void print_my_struct (struct my_struct *msp) {
printf ("%d %c %s %f\n", msp->fielda, msp->fieldb, msp->fieldc,
msp->fieldd);
}
Results in the following output:
length of ms = 16
1 a This is a null terminated string 3.140000
The assignment "ms.fieldc = my_string;" is not actually a string move, but
is instead a pointer assignment. In other words, my_string and ms.fieldc
both point to a null terminated string "This is a null terminated string".
On the other hand, you could define my_struct like this:
struct my_struct {
int fielda;
char fieldb;
char fieldc[80];
float fieldd;
};
And change the string pointer assignement above to:
strcpy (ms.fieldc, "This is another null terminated string");
In this case the structure contains the entire 80 byte string, not just a
pointer to it. This is more like how COBOL currently works (except that the
string is not null-terminated, of course).
length of ms = 92
1 a This is another null terminated string 3.140000
>Do they allow them as ARRAY (table) elements?
Yes... But this may be an incomplete answer...
>What about those that have "prefixed" (half-word or other binary "length)
>fields - similar to SQL VARCHAR) in these places?
I think Turbo Pascal worked this way. Not sure about Delphi.
Anyway, here's my uneducated guess about how COBOL ANY LENGTH is supposed to
work...
01 my-group.
field-a binary-short.
field-b pic x.
field-c pic x any length limit 80 delimited by x'00'.
field-d float-short.
move 1 to field-a
move 'a' to field-b
move "This is a null terminated string" to field-c
move 3.14 to field-d
This would be similar to the second C example. my-group is 90(?) bytes in
length. (4 + 1 + 81 + 4) Not really sure on that part...
Anyway, if INDIRECT were specified then, well honestly, I'm not quite sure.
I will think on it...
Frank
---
Frank Swarbrick
Senior Developer/Analyst - Mainframe Applications
FirstBank Data Corporation - Lakewood, CO USA
| |
| Sergey Kashyrin 2006-10-12, 6:55 pm |
| Bill,
I think Frank gave a good C sample but C is weak and dangerous. I'll
recommend to use C++ anyway (even for C programs).
I don't know how exactly how Cobol "any length" should work.
I hope it's not as a dynamic arrays in PL/I (i.e. M(*) and later ALLOCATE
M(N)).
I expect DIRECT should have a limit and in this case memory allocation of
"01 F pic x any length limit 80" is not different from "01 F pic x(80)",
i.e. it gets 80 bytes.
So the C/C++ equivalent will be "char F[80]"
INDIRECT is probably pointer to the memory allocated elsewhere, i.e. in
C/C++ "char * F" and you can allocate it anytime from the heap "F = new
char[sizeexpression]"
In JAVA you don't have static allocation of non-primitive variables, i.e.
the array in Java is OBJECT and you can only have a reference to the object.
Also there is no such thing as "LENGTH OF" or "sizeof" of the object
So for
01 STRUCT.
02 N PIC S9(4) COMP.
02 F PIC X(80).
There are no Java or .NET equivalents of this structure.
The closest in Java will be
class C {
short N;
byte [] F = new byte[80];
}
And if you have 2 variables:
C c1 = new C();
C c2 = new C();
you can't do "c1 = c2" like "move c2 to c1" in Cobol because you'll copy the
reference and not the memory area.
In C++ you can.
> Do they allow "null-terminated" strings in the middle of "structures"?
In C++
struct C {
short N;
char F[80];
...
char Fn[80];
}
each of those F take 80 bytes, so you can put 0-terminated string into each
of them which is up to 79 bytes (+ 0-value)
In C in this case you can't do
F = "abc"
You can only use the fuction
strcpy(F, "abc")
and this function will not check if the value it's bigger !!! you'll just
override extra memory !
But YES, you have a lot of different 0-terminated strings in the middle of
the structure and some memory is wasted.
In Java there is no such thing as a "middle of the structure". String is the
object which is different from the array of bytes and it's not 0-terminated.
All SQL VARCHAR data is mapped to this "String" objects with the exception
of DB2 "VARCHAR(n) FOR BIT DATA"
which should be mapped to the array of bytes (actually you can read VARCHAR
column into array of bytes anyway).
> Do they allow them as ARRAY (table) elements?
In Java there are no "arrays of objects". There only arrays of the
references to the object (read POINTERs).
But there are arrays of the primitive types (i.e. byte, char(which is 2
bytes in java), short, int, long, float, double)
I don't think my explanations were good, but I've tried :-)
Regards,
Sergey
"William M. Klein" <wmklein@nospam.netcom.com> wrote in message
news:1_VWg.254231$Pi2.105476@fe08.news.easynews.com...
> In the Standards work on the next (2008 or so) COBOL Standard, they have
> introduced a NEW "elementary any length data item". (The '02 Standard has
> ANY LENGTH 01-levels).
>
> There are two types of ANYLENGTH items, one includes the data "inline"
> (DIRECT) and the other has it stored elsewhere (INDIRECT). To me, the
> DIRECT format has some POTENTIAL problems and (fairly closely) resembles
> those existing COBOL implementations with an EXTENSION that allows for
> nested ODO (OCCURS DEPENDING ON) or data after an ODO.
>
> My OFT-TOPIC question to this group is whether any of you can answer the
> question of what OTHER languages (particularly C/C++ and Java) do as far
> as "structures" go.
>
> Do they allow "null-terminated" strings in the middle of "structures"?
>
> Do they allow them as ARRAY (table) elements?
>
> What about those that have "prefixed" (half-word or other binary "length)
> fields - similar to SQL VARCHAR) in these places?
>
> --
> Bill Klein
> wmklein <at> ix.netcom.com
>
|
|
|
|
|