Page 1 of 1

offsetof() equivalent in PL/I?

Posted: Tue Oct 03, 2017 5:19 am
by peeterjoot
The SIZE (or STG, or STORAGE) builtin function appears to be the PL/I equivalent of the C sizeof() builtin.

If I have a PL/I structure, such as:

DCL 1 MYSTRUCT,
5 F1 CHAR(13),
5 F2 FIXED BIN(31),
5 F3 FIXED DECIMAL(5,2),
5 F4 FIXED BIN(15),
5 F5 POINTER,
5 F6 CHAR(7);

Is there a way to do the equivalent of the C offsetof(MYSTRUCT,F6) operation for this structure without assuming the value of SIZE() for the POINTER field, nor adding up all the sizes that precede it (along with any alignment padding which may or may not be inserted by the PL/I compiler).

I see there is an OFFSET() builtin associated with AREAs, but that looks like it's a different idea.

I would guess (but haven't tried) that a runtime calculation using POINTERs and FIXED BIN variables of appropriate sizes BASED to those POINTERs could perform this offsetof calculation at runtime, but that seems like a very hard way to do this, if the language even allows it.

Re: offsetof() equivalent in PL/I?

Posted: Tue Oct 03, 2017 6:37 pm
by Akatsukami
It's been a couple of decades since I wrote any C, so I may not properly understand what you're looking for, but I think you want the LOCATION function.

Re: offsetof() equivalent in PL/I?

Posted: Tue Oct 03, 2017 9:44 pm
by prino
offset = addr(mystruct.f6) - addr(mystruct.f1);

Re: offsetof() equivalent in PL/I?

Posted: Thu Oct 05, 2017 4:02 pm
by peeterjoot
Akatsukami wrote: Tue Oct 03, 2017 6:37 pm It's been a couple of decades since I wrote any C, so I may not properly understand what you're looking for, but I think you want the LOCATION function.
Thank you, that looks like the PL/I equivalent to C's offsetof().