Search code examples
pointersibm-midrangerpgle

dealloc + %alloc() vs %realloc() - RPGLE


Knowing that ptr <> *null, are those two pieces of code functionally identical?

dealloc(en) ptr;
ptr = %alloc(500);

and

ptr = %realloc(ptr:500);

Solution

  • No.

    %realloc() saves the current value of the storage pointed to by ptr.

    So if there's not enough space to enlarge the existing space, thus returning the same value for ptr. The system will copy the bytes from the original space to the new space.

    From the manual:
    %REALLOC (Reallocate Storage)

    The heap storage pointed to by the returned pointer has the same value as the heap storage pointed to by ptr. If the new length is longer than the old length, the additional storage is uninitialized.