× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



Hi Dan,

Interesting. You allocated space for 16 elements, then proceed to populate 101 elements. There's no error because you told RPG there are 50,000 elements. Having blasted past the space you allocated, who knows what got clobbered?

Keep track of the number of elements allocated, and don't exceed that when populating elements of the array.

--
*Peter Dow* /
Dow Software Services, Inc.
909 793-9050
petercdow@xxxxxxxxx <mailto:petercdow@xxxxxxxxx>
pdow@xxxxxxxxxxxxxx <mailto:pdow@xxxxxxxxxxxxxx> /


On 3/7/2017 9:43 PM, Dan wrote:
I am looking at dynamically sized arrays to solve a problem that's been
biting us in the ass lately with attempts to add an element to an array
past the number of elements defined for the array. Currently, our arrays
handle 99.999% of the volume we throw at them, but then we get an outlier
that exceeds the definition and creates a whopper of a headache for us.

So, Google is my friend, and I found
https://enskill.com/faqs/dynamic-memory-allocation-and-arrays/. I modified
it as shown below. I added dumps to the original code to see how the
dynamic array is shown on the dump. What I found was dimensions for Array
that were much higher than what I apparently allocated them for. The
initial allocation provides for 16 elements, but the dump after that
allocation (but before the reallocation) shows 226 elements. After I
reallocate to provide for 32 elements, the dump shows 209 elements. Both
dumps list the contents of Array as "NOT ADDRESSABLE". (Dumps shown
below.) Can anyone explain what's going on here?


d ArrayPtr s * Inz( *Null )
d Array s 31 0 Dim( 50000 )
d Based( ArrayPtr )
d ArrayElems s 7 0 Inz( 0 )
d TestValue s 31 0

// Allocate memory for 16 elements
Arrayptr = %Alloc(%Size(Array) * 16);
// ... (You can now process up to 16 elements of the array.)

Array( 1 ) = 1;
For ArrayElems = 2 to 101;
Array( ArrayElems ) = Array( ArrayElems - 1 ) * 2;
Endfor;

TestValue = Array( 101 );
Dump(A) 'First one';

// Allocate memory for 32 elements
Arrayptr = %Realloc(Arrayptr: %Size(Array) * 32);
// ... (You can now process up to 32 elements.)
Dump(A) 'Second one';

*inLR = *on;

Dumps:
First one
NAME ATTRIBUTES
VALUE
ARRAY PACKED(31,0)
DIM(226)
(1-226) NOT
ADDRESSABLE
ARRAYELEMS PACKED(7,0) 0000102.
'0000102F'X
ARRAYPTR POINTER
SPP:DA1BF703050021E0
TESTVALUE PACKED(31,0)
1267650600228229401496703205376.
VALUE IN HEX
'1267650600228229401496703205376F'X
* * * * * E N D O F R P G D U M P * * * *
*

Second one
NAME ATTRIBUTES
VALUE
ARRAY PACKED(31,0)
DIM(209)
(1-209) NOT
ADDRESSABLE
ARRAYELEMS PACKED(7,0) 0000102.
'0000102F'X
ARRAYPTR POINTER
SPP:DA1BF703050022F0
TESTVALUE PACKED(31,0)
1267650600228229401496703205376.
VALUE IN HEX
'1267650600228229401496703205376F'X
* * * * * E N D O F R P G D U M P * * * *
*


As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:
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.