Topic
• 6 replies
• Latest Post - ‏2014-12-02T15:54:28Z by Robin400
markboonie
9 Posts

# Pinned topic Calculating number of array elements

‏2013-06-25T18:38:22Z | array dynamic element

I'm trying to represent a data structure that consists of a header followed by a number of array elements.  I'd like the number of array elements to be as large as possible while not exceeding 4096 (including the header).  Is it possible to use the SIZE() function for this?  I'm getting message IBM1748I S, "Extents of automatic variables must not depend on the extents of automatic variables declared later in the same block."  My declaration is al follws:

Dcl
1 top1 Structure ,
2 mid1,
3 low1 Unsigned Bin Fixed (32),
3 low2 Char (8),
2 mid2 ((4096 - size(mid1)) / size(mid2)),
3 low3 Unsigned Bin Fixed (64),
3 low4 Char (48);

It appears to me that the size of mid2 should be able to be calculated first and then used in the calculation for the number of array elements.  Any ideas?

- mb

• brataj
11 Posts

#### Re: Calculating number of array elements

‏2013-06-25T19:59:50Z

mid2 is an array, so you can't use it to define it's own size... but I think it goes further than that, as being a structure member the size can't really be determined until after all the alignment has been taken care of.  But size(mid2(1)) doesn't work either, I suspect because the compiler isn't as devious as humans.

Anyway, this sort of thing works:

dcl
1   aMid1,
3 low3 Unsigned Bin Fixed (32),
3 low4 Char (8);
dcl
1   aMid2,
3 low3 Unsigned Bin Fixed (64),
3 low4 Char (48);

Dcl
1 top1 Structure ,
2 mid1 like aMid1,
2 mid2 ((4096 - size(aMid1)) / size(aMid2)) like aMid2;

Possibly there are other variants.

• brataj
11 Posts

#### Re: Calculating number of array elements

‏2013-06-25T19:59:50Z

mid2 is an array, so you can't use it to define it's own size... but I think it goes further than that, as being a structure member the size can't really be determined until after all the alignment has been taken care of.  But size(mid2(1)) doesn't work either, I suspect because the compiler isn't as devious as humans.

Anyway, this sort of thing works:

dcl
1   aMid1,
3 low3 Unsigned Bin Fixed (32),
3 low4 Char (8);
dcl
1   aMid2,
3 low3 Unsigned Bin Fixed (64),
3 low4 Char (48);

Dcl
1 top1 Structure ,
2 mid1 like aMid1,
2 mid2 ((4096 - size(aMid1)) / size(aMid2)) like aMid2;

Possibly there are other variants.

• brataj
11 Posts

#### Re: Calculating number of array elements

‏2013-06-25T20:01:22Z
• brataj
• ‏2013-06-25T19:59:50Z

mid2 is an array, so you can't use it to define it's own size... but I think it goes further than that, as being a structure member the size can't really be determined until after all the alignment has been taken care of.  But size(mid2(1)) doesn't work either, I suspect because the compiler isn't as devious as humans.

Anyway, this sort of thing works:

dcl
1   aMid1,
3 low3 Unsigned Bin Fixed (32),
3 low4 Char (8);
dcl
1   aMid2,
3 low3 Unsigned Bin Fixed (64),
3 low4 Char (48);

Dcl
1 top1 Structure ,
2 mid1 like aMid1,
2 mid2 ((4096 - size(aMid1)) / size(aMid2)) like aMid2;

Possibly there are other variants.

I see the new and improved communities apparently count posts by community...

• markboonie
9 Posts

#### Re: Calculating number of array elements

‏2013-06-26T13:07:12Z
• brataj
• ‏2013-06-25T19:59:50Z

mid2 is an array, so you can't use it to define it's own size... but I think it goes further than that, as being a structure member the size can't really be determined until after all the alignment has been taken care of.  But size(mid2(1)) doesn't work either, I suspect because the compiler isn't as devious as humans.

Anyway, this sort of thing works:

dcl
1   aMid1,
3 low3 Unsigned Bin Fixed (32),
3 low4 Char (8);
dcl
1   aMid2,
3 low3 Unsigned Bin Fixed (64),
3 low4 Char (48);

Dcl
1 top1 Structure ,
2 mid1 like aMid1,
2 mid2 ((4096 - size(aMid1)) / size(aMid2)) like aMid2;

Possibly there are other variants.

Interesting.  I would have thought that mid2 was the name not of the array but of the individual element(s) of which the array is composed.  That would explain the error message -- the code is trying to use the SIZE() of the array to define the array itself.  (The meaning of the message is still cryptic to me.  I'll go back now and try to decipher it.)

Thanks for the bypass, though.  It's more logical, too -- define the smaller structures first, and then define the larger structures as compositions of the smaller ones.

- mb

• brataj
11 Posts

#### Re: Calculating number of array elements

‏2013-06-26T13:34:35Z

Interesting.  I would have thought that mid2 was the name not of the array but of the individual element(s) of which the array is composed.  That would explain the error message -- the code is trying to use the SIZE() of the array to define the array itself.  (The meaning of the message is still cryptic to me.  I'll go back now and try to decipher it.)

Thanks for the bypass, though.  It's more logical, too -- define the smaller structures first, and then define the larger structures as compositions of the smaller ones.

- mb

Both the dimension(s) and type are bound to a name...

"declared later in the same block" is I believe applicable in this case because the compiler hasn't yet "committed" to some or all of the structure that you're in the process of defining, so it presumes a later definition is intended.

Bernie

• Robin400
42 Posts

#### Re: Calculating number of array elements

‏2013-06-27T08:34:26Z

It's necessary to specify the number of elements for the array.

Normally this is specified as a constant (e.g., declare x(25) float; )

or as a variable whose value is read in or computed

(e.g., get (m); begin; declare x(m) float; ... )

• Robin400
42 Posts

#### Re: Calculating number of array elements

‏2014-12-02T15:54:28Z

Further to the explanation of the error message:- the expression

(4096 - size(mid1)) / size(mid2)

as part of the declaration of the array

mid2 ((4096 - size(mid1)) / size(mid2))

requires the size of <mid2> which isn't known at the time that

the expression is evaluated.