• Is this a compiler bug ?

    From Rod Kay@21:1/5 to All on Sun Mar 19 17:17:20 2023
    Hi all,

    Came across this during a port of the Box2D physics engine.

    It's a generic Stack package using 'ada.Containers.Vectors' to
    implement the stack.

    One generic parameter is the 'initial_Capacity' of the stack, used
    in the 'to_Stack' construction function, via the Vectors
    'reserve_Capacity' procedure.

    In the 'to_Stack' function, the Capacity is reserved correctly but
    in the test program when the stack is created and assigned to a
    variable, the capacity is 0.

    Here is the (very small) source code ...

    https://gist.github.com/charlie5/7b4d863227a510f834c2bfd781dd50ba


    The output I get with GCC 12.2.0 is ...

    [rod@orth bug]$ ./stack_bug
    to_Stack ~ Initial Capacity: 256
    to_Stack ~ Before reserve: 0
    to_Stack ~ After reserve: 256
    stack_Bug ~ Actual Capacity: 0


    Regards.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jeffrey R.Carter@21:1/5 to Rod Kay on Sun Mar 19 11:33:50 2023
    On 2023-03-19 07:17, Rod Kay wrote:

       In the 'to_Stack' function, the Capacity is reserved correctly but in the
    test program when the stack is created and assigned to a variable, the capacity
    is 0.

    I think this is acceptable behavior. See ARM A.18.2 (147.19/3, 147.20/3, & 147.b/3) (http://www.ada-auth.org/standards/aarm12_w_tc1/html/AA-A-18-2.html). The first two sections define the behavior of procedure Assign, while the last states "Assign(A, B) and A := B behave identically".

    Assign (A, B) only changes the capacity of A if A.Capacity < B.Length.

    So if the compiler does not use build-in-place for the initialization of the variable, then the assignment of the function result should not change the capacity of the variable from its (apparent) default of zero (there is, of course, no requirement for the capacity of a default-initialized vector).

    The discussion of capacities for vectors is only meaningful for a subset of possible implementations, so messing with capacities may have no meaningful effect at all.

    For an unbounded stack based on a linked list (with no concept of capacity) you could use PragmARC.Data_Structures.Stacks.Unbounded.Unprotected (https://github.com/jrcarter/PragmARC/blob/Ada-12/pragmarc-data_structures-stacks-unbounded-unprotected.ads).

    --
    Jeff Carter
    "Since I strongly believe that overpopulation is by
    far the greatest problem in the world, this [Soylent
    Green] would be my only message movie."
    Charleton Heston
    123

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rod Kay@21:1/5 to Jeffrey R.Carter on Mon Mar 20 13:24:40 2023
    On 19/3/23 21:33, Jeffrey R.Carter wrote:
    On 2023-03-19 07:17, Rod Kay wrote:

        In the 'to_Stack' function, the Capacity is reserved correctly but
    in the test program when the stack is created and assigned to a
    variable, the capacity is 0.

    I think this is acceptable behavior. See ARM A.18.2 (147.19/3, 147.20/3,
    & 147.b/3) (http://www.ada-auth.org/standards/aarm12_w_tc1/html/AA-A-18-2.html).
    The first two sections define the behavior of procedure Assign, while
    the last states "Assign(A, B) and A := B behave identically".

    Assign (A, B) only changes the capacity of A if A.Capacity < B.Length.

    So if the compiler does not use build-in-place for the initialization of
    the variable, then the assignment of the function result should not
    change the capacity of the variable from its (apparent) default of zero (there is, of course, no requirement for the capacity of a default-initialized vector).

    The discussion of capacities for vectors is only meaningful for a subset
    of possible implementations, so messing with capacities may have no meaningful effect at all.

    For an unbounded stack based on a linked list (with no concept of
    capacity) you could use
    PragmARC.Data_Structures.Stacks.Unbounded.Unprotected (https://github.com/jrcarter/PragmARC/blob/Ada-12/pragmarc-data_structures-stacks-unbounded-unprotected.ads).



    Thank you, Jeffrey, for the detailed reply.

    I'm now using a limited record with an extended return for
    'build-in-place' initialisation and am getting the behavior I desired.


    Cheers.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)