Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
*PROB* Using Segments to mess with local arrays:
#1
Okay I made a little test program that can create an array inside a SUB, get the segment address (assuming the offset is 0) and the mess with that array ANYWHERE else in the entire program.

I personally thought this was the most kickass thing in the world (because it's so new to me and) because I don't even need an array's fricken name in order to use it!!!! This is particularly handy when you need an array that you can resize the dimensions of (REDIM for use with maps that vary in size) and when that array is inside a TYPE (because you can't do that with static arrays unfortunately).

Unfortunately, it is a mystery to me why my attempt to impliment this into my heavily-UGL utilizing program failed.

SEGMENTS.BAS
Code:
DECLARE FUNCTION GetInfo% (map AS ANY, x%, y%)
DECLARE SUB ShowMatrix (map AS ANY)
DECLARE SUB SetInfo (map AS ANY, x%, y%, value%)
DECLARE SUB ChangeArray (map AS ANY)
TYPE t
        mapseg AS INTEGER
        maxx AS INTEGER
        maxy AS INTEGER
END TYPE

DIM map AS t

REDIM array(0 TO 10, 0 TO 10) AS INTEGER
map.mapseg = VARSEG(array(0, 0))

map.maxx = 10
map.maxy = 10

SetInfo map, 13, 0, 4

ShowMatrix map
SLEEP

ChangeArray map

SetInfo map, 3, 2, 7

ShowMatrix map

SUB ChangeArray (map AS t)
        REDIM array(0 TO 3, 0 TO 3) AS INTEGER
        map.mapseg = VARSEG(array(0, 0))

        map.maxx = 3
        map.maxy = 3
END SUB

FUNCTION GetInfo% (map AS t, x%, y%)
        DEF SEG = map.mapseg
        GetInfo% = PEEK((y% * (map.maxx + 1)) + x%)
        DEF SEG
END FUNCTION

SUB SetInfo (map AS t, x%, y%, value%)
        DEF SEG = map.mapseg
        POKE (y% * (map.maxx + 1)) + x%, value%
        DEF SEG
END SUB

SUB ShowMatrix (map AS t)
        DIM x%, y%
        CLS
        FOR y% = 0 TO map.maxy
                FOR x% = 0 TO map.maxx
                        PRINT GetInfo%(map, x%, y%);
                NEXT x%
                PRINT CHR$(13) + CHR$(13)
        NEXT y%
END SUB

As I look over this code after I had posted this message, I realize that I may have actually sort of "cheated" with this. I noticed that I didn't make the array a global one, so I'm starting to think that when it changes the array from inside that sub, it's actually leaving behind that old code, and simply allocating extra space. If this is the case, the code may be terribly inefficient, and I'll have to look it over again. Sorry in advance for any possible inconvience!

P.S. boy golley do i sound weird when i tries t' sound profeshunal
earn.
Reply
#2
Okay I think this may be a little better for gathering evidence for/against my theory:

Code:
DECLARE FUNCTION GetInfo% (map AS ANY, x%, y%)
DECLARE SUB ShowMatrix (map AS ANY)
DECLARE SUB SetInfo (map AS ANY, x%, y%, value%)
DECLARE SUB CreateArray (map AS ANY)
DECLARE SUB ChangeArray (map AS ANY)
TYPE t
        mapseg AS INTEGER
        maxx AS INTEGER
        maxy AS INTEGER
END TYPE

DIM map AS t

RANDOMIZE TIMER
FOR i% = 1 TO 5
        ChangeArray map

        SetInfo map, INT(RND * map.maxx) + 1, INT(RND * map.maxy) + 1, 4

        ShowMatrix map
        PRINT map.mapseg
        SLEEP 1
NEXT i%

SUB ChangeArray (map AS t)
        map.maxx = INT(RND * 20) + 1
        map.maxy = INT(RND * 20) + 1

        REDIM array(0 TO map.maxx, 0 TO map.maxy)  AS INTEGER
        map.mapseg = VARSEG(array(0, 0))
END SUB

FUNCTION GetInfo% (map AS t, x%, y%)
        DEF SEG = map.mapseg
        GetInfo% = PEEK((y% * (map.maxx + 1)) + x%)
        DEF SEG
END FUNCTION

SUB SetInfo (map AS t, x%, y%, value%)
        DEF SEG = map.mapseg
        POKE (y% * (map.maxx + 1)) + x%, value%
        DEF SEG
END SUB

SUB ShowMatrix (map AS t)
        DIM x%, y%
        CLS
        FOR y% = 0 TO map.maxy
                FOR x% = 0 TO map.maxx
                        PRINT GetInfo%(map, x%, y%);
                NEXT x%
                PRINT CHR$(13) + CHR$(13)
        NEXT y%
END SUB
earn.
Reply
#3
After you create an array in a sub, the array dissappears once the program exits the sub.

Try making the array STATIC.
Reply
#4
Can you still REDIM a STATIC array? I thought you couldn't?
earn.
Reply
#5
I just tested it. You can REDIM a STATIC array as long as it's dynamic.
Reply
#6
Now I have a question about dynamic arrays... First, however, I'm gonna tell you the facts, as far as I know them. If ever 'm wrong, please explain why. Thanks.

- Dynamic arrays are located at the same exact part of memory throughout your entire program.
- You can access them throughout any SUB or FUNCTION as long as you have the segment address for it, because they don't move within memory.

If those are true, then how are they different from SHARED arrays? I ask this because as a temporary solution, I've resorted to using one single shared array for my map, and I REDIM this map each time a level is loaded.

So my question really is just, are dynamic arrays any better than shared arrays?
earn.
Reply
#7
all arrays declared inside subs/functions are dynamic.
y smiley is 24 bit.
[Image: anya2.jpg]

Genso's Junkyard:
http://rel.betterwebber.com/
Reply
#8
That doesn't answer my question of SHARED vs DYNAMIC. Sad

*BUMPA$$B1TCH*
earn.
Reply
#9
EDIT: Oh wait, I just realized you probably misunderstood me. I meant declare it STATIC like this:
Code:
SUB mySub
STATIC myArray()
DIM myArray(1234 TO 4321)
...
I wasn't talking about '$STATIC. Sorry, I should have made myself more clear.

Original post (before edit):
Quote:'$DYNAMIC arrays can move when you REDIM them, but overwise they stay put.

The same is true of '$DYNAMIC SHARED arrays. '$STATIC SHARED arrays never move, neither do unshared '$STATIC arrays.

And Rel's right. Any array in a SUB/FUNCTION (STATIC or not) is '$DYNAMIC, not '$STATIC (I forgot about that), meaning it isn't dimmensioned until the SUB/FUNCTION gets called and actually DIMs it. If it's STATIC it shouldn't move around unless it gets REDIMed. If it's not STATIC it will be automatically ERASEd when the SUB/FUNCTION it's in is exited and it's data won't be retained between calls to the SUB/FUNCTION.
Reply
#10
Soooo then declaring my array STATIC within the array will solve this problem?

Also, what is the advantage of using this method rather than just declaring the array SHARED in the beginning of the program? Does it save space somehow?
earn.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)