Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ALLOC Command & The %ALLOC BIF

Status
Not open for further replies.

Skittle

ISP
Sep 10, 2002
1,528
US
Can anybody explain to me what the ALLOC command does?
It seems to define a pointer address to look at a set area of space.

Could somebody explain to me where the storage space exists and how it is used differently to a variable in RPG?




 
This is what the manual says about %Alloc

%ALLOC(num)

%ALLOC returns a pointer to newly allocated heap storage of the length specified. The newly allocated storage is uninitialized.

The parameter must be a non-float numeric value with zero decimal places. The length specified must be between 1 and 16776704.

For more information, see Memory Management Operations.

If the operation cannot complete successfully, exception 00425 or 00426 is issued.

Figure 165. %ALLOC Example


*..1....+....2....+....3....+....4....+....5....+....6....+....7...+....
/FREE
// Allocate an area of 200 bytes
pointer = %ALLOC(200);
/END-FREE

This is what "Memory Management Operations" says about the ALLOC opcode:

The ALLOC operation allocates heap storage and sets the result-field pointer to point to the storage. The storage is uninitialized.

Storage is implicitly freed when the activation group ends. Setting LR on will not free any heap storage allocated by the module, but any pointers to heap storage will be lost.

Misuse of heap storage can cause problems. The following example illustrates a scenario to avoid:




D Fld1 S 25A BASED(Ptr1)
D Fld2 S 5A BASED(Ptr2)
D Ptr1 S *
D Ptr2 S *
....
C ALLOC 25 Ptr1
C DEALLOC Ptr1
* After this point, Fld1 should not be accessed since the
* basing pointer Ptr1 no longer points to allocated storage.
C CALL 'SOMEPGM'

* During the previous call to 'SOMEPGM', several storage allocations
* may have been done. In any case, it is extremely dangerous to
* make the following assignment, since 25 bytes of storage will
* be filled with 'a'. It is impossible to know what that storage
* is currently being used for.
C EVAL Fld1 = *ALL'a'




Following are more problematic situations:

A similar error can be made if a pointer is copied before being reallocated or deallocated. Great care must be taken when copying pointers to allocated storage, to ensure that they are not used after the storage is deallocated or reallocated.
If a pointer to heap storage is copied, the copy can be used to deallocate or reallocate the storage. In this case, the original pointer should not be used until it is set to a new value.
If a pointer to heap storage is passed as a parameter, the callee could deallocate or reallocate the storage. After the call returns, attempts to access the storage through pointer could cause problems.
If a pointer to heap storage is set in the *INZSR, a later RESET of the pointer could cause the pointer to get set to storage that is no longer allocated.
Another type of problem can be caused if a pointer to heap storage is lost (by being cleared, or set to a new pointer by an ALLOC operation, for example). Once the pointer is lost, the storage it pointed to cannot be freed. This storage is unavailable to be allocated since the system does not know that the storage is no longer addressable. The storage will not be freed until the activation group ends.

Looks confusing to me, hope it helps.



RedMage1967
IBM Certifed - RPG IV Progammer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top