Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Passing fixed length string array to function
#11
It seems to work just fine too. Here's the old test bed I developed it on with your changes.

Code: (Select All)
'Extracting a mem element
SCREEN _NEWIMAGE(800, 600, 32)
DO: LOOP UNTIL _SCREENEXISTS
_SCREENMOVE 10, 10
CLS

TYPE sheepdip
    a AS INTEGER
    b AS INTEGER
    c AS INTEGER
    d AS LONG
END TYPE

REDIM Ar(15) AS sheepdip
REDIM D(15) AS STRING * 5
REDIM E(15) AS LONG

'load the array with values
heading
FOR x% = 0 TO 15
    Ar(x%).a = x%: Ar(x%).b = Ar(x%).a * 2: Ar(x%).c = Ar(x%).a * 3
    Ar(x%).d = Ar(x%).a ^ 2
    D(x%) = "dog" + _TRIM$(STR$(x%)): E(x%) = x% ^ 3
    PRINT x%, Ar(x%).a, Ar(x%).b, Ar(x%).c, Ar(x%).d, D(x%), E(x%)
NEXT x%

DO
    INPUT "take away #? ", t%
    IF t% > UBOUND(Ar) OR t% < LBOUND(Ar) THEN EXIT DO 'out of range number ends program

    DIM m AS _MEM: m = _MEM(Ar()): MemSmush m, t%: _MEMFREE m
    DIM m1 AS _MEM: m1 = _MEM(D()): MemSmush m1, t%: _MEMFREE m1
    DIM m2 AS _MEM: m2 = _MEM(E()): MemSmush m2, t%: _MEMFREE m2
    REDIM _PRESERVE Ar(UBOUND(Ar) - 1) AS sheepdip
    REDIM _PRESERVE D(UBOUND(D) - 1) AS STRING * 5
    REDIM _PRESERVE E(UBOUND(E) - 1) AS LONG

    PRINT
    heading
    FOR x% = 0 TO UBOUND(Ar)
        PRINT x%, Ar(x%).a, Ar(x%).b, Ar(x%).c, Ar(x%).d, D(x%), E(x%)
    NEXT x%
LOOP
END


SUB MemSmush (m AS _MEM, x%) 'by OldMoses w/ optimization by Steve McNeill
    'The new way
    DIM o AS _OFFSET
    o = (m.ELEMENTSIZE * x%)
    _MEMCOPY m, m.OFFSET + o + m.ELEMENTSIZE, m.SIZE - o - m.ELEMENTSIZE TO m, m.OFFSET + o '<-- 3 multiplication calculations and 1 negation less than previously used
    _MEMFILL m, m.OFFSET + m.SIZE - m.ELEMENTSIZE, m.ELEMENTSIZE, 0 AS _BYTE '<-- Removed multiplication and division operation

    'The old way
    '_MEMCOPY m, m.OFFSET + (m.ELEMENTSIZE * (x% + 1)), m.SIZE - (m.ELEMENTSIZE * (x% + 1)) TO m, m.OFFSET + (m.ELEMENTSIZE * x%)
    '_MEMFILL m, m.OFFSET + m.ELEMENTSIZE * ((m.SIZE / m.ELEMENTSIZE) - 1), m.ELEMENTSIZE, 0 AS _BYTE
END SUB

SUB heading
    PRINT "#", "n", "n x 2", "n x 3", "n ^ 2"
END SUB
DO: LOOP: DO: LOOP
sha_na_na_na_na_na_na_na_na_na:
Reply


Messages In This Thread
RE: Passing fixed length string array to function - by OldMoses - 08-25-2022, 11:45 AM



Users browsing this thread: 2 Guest(s)