Am 16.04.2024 um 17:12 schrieb Greg Wilburn <gwilburn@xxxxxxxxxxxxxxxxxxxxxxx>:

I like to use VARCHAR when loading data into data structures that are transformed to JSON for APIs. Both structures are defined with INZ. Does that not really initialize all the subfields?

I hope I can clear this up a bit.

The problem is the varchar data type - lets have a look in detail.

dcl-c myVarChar varchar(50) inz;

The compiler does allocate 52 bytes of (static) memory and it initializes the first two bytes (the length bytes) with a value of x'0000' - meaning binary zero. The remaining 50 bytes of memory are AFAIK not initialized - so you can make no assumptions about the remaining 50 bytes.

myVarChar = 'ABCDE';

Now the compiler sets the memory to 2 bytes with the value x'0005' (binary for length=5) and the next 5 bytes to 'ABCDE'. The remaining bytes are still undefined.

myVarChar = 'ABC';

Here compiler sets the memory to 2 bytes with the value x'0003' (binary for length=3) and the next 3 bytes to 'ABC'. Its very probable that the next two bytes still contain 'DE' - but you can't be sure of that - they are undefined.

Now put such varchar fields into a data structure - they will behave exactly the same.

if myVarChar = 'ABCEDF';

If you compare those varchar directly, the compiler only compares up to the length of the shorter of the two strings - it will never compare the undefined remaining bytes. In fact the compiler may first only compare the length bytes - if those are not equal, the strings can't be equal.

But if you compare two data structures, the whole 52 bytes of a varchar(50) field in both structures will be compared, because they are inside the structures, and a data structure compare always compares the whole length. But you can make no assumptions about the content of the remaining bytes - so even if you assign the exact same values to two separate data structures (whether in arrays or not) you will end up with two different data structures, if the contain varchar fields.

I hope that this may help you. If I wrote something incorrect I would be glad if someone (Barbara? Jon?) may correct me, but I'm pretty sure, that the remaining bytes of the varchar fields are your problem.

HTH and kind regards,
Daniel

As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.