CLEO Help About sampfuncs arrays (or just arrays in general)

CLEO related
Status
Not open for further replies.

Zin

Expert
Joined
Aug 1, 2013
Messages
1,734
Solutions
2
Reaction score
117
I'm looking to create some arrays in sampfuncs and then store the array to a global var so I can get the array later on and simplify the code better, however I'm unsure as if this is possible as I made a test script to test this principle and it seems like the array address changes when it is stored. So I'm wondering if me clearing the memory in the var I created the array in deletes the entire array or something, script sort of would explain it better.
Code:
{$CLEO .cs}

THREAD 'TEST'

REPEAT
    WAIT 0
UNTIL 0AFA:

alloc 31@ 1024
0C20: string_array 31@ element 1 size 256 = "1000.570557 1638.594849 10.480600"
0C20: string_array 31@ element 2 size 256 = "1500.875487 1754.457845 10.834575"
0C20: string_array 31@ element 3 size 256 = "2500.875487 1638.594849 14.834575"
0BFC: set_global_var "1" = 31@
chatmsg "1: %x" 0xFFFFFF 31@
31@ = 0
free 31@

alloc 31@ 1024
0C20: string_array 31@ element 1 size 256 = "-1000.570557 -1638.594849 -10.480600"
0C20: string_array 31@ element 2 size 256 = "-1500.875487 -1754.457845 -10.834575"
0C20: string_array 31@ element 3 size 256 = "-2500.875487 -1638.594849 -14.834575"
0BFC: set_global_var "2" = 31@
chatmsg "2: %x" 0xFFFFFF 31@
31@ = 0
free 31@

0B34: samp register_client_command "TEST" to_label @TEST

WHILE TRUE
    WAIT 0
    0AB1: @RESTART 1 WITH_KEY 88 // KEY_X   
END


:TEST
0B35: samp 0@ = get_last_command_params
IF
    0AD4: $NOT_USED scan 0@ format "%d %d" 0@ 1@
THEN
    alloc 0@ 1024
    IF
        0@ == 1
    THEN
        0BFD: 0@ = get_global_var "1"
    ELSE
        IF
            0@ == 2
        THEN
            0BFD: 0@ = get_global_var "2"
        END
    END
    chatmsg "%x" 0xFFFFFF 0@
    0C1F: 1@ = string_array 0@ element 1@ size 256
    chatmsg 1@ 0xFFFFFF
    free 0@
END
samp.CmdRet()



:RESTART
IF
0AB0: 0@
THEN
    REPEAT
        WAIT 0
        PRINT "~R~RESTART: ~w~TEST.CS" 100
    UNTIL 8AB0: 0@
    0A92: RESTART "TEST.CS"
    004E: STOP THIS CLEO
END
0AB2: 0
 

monday

Expert
Joined
Jun 23, 2014
Messages
1,127
Solutions
1
Reaction score
158
it looks like you're deallocating memory using free 31@, so the global var 1 points to memory that can be occupied by other stuff

idk if that's the only issue here though
 
  • Like
Reactions: Zin

Zin

Expert
Joined
Aug 1, 2013
Messages
1,734
Solutions
2
Reaction score
117
Those were my instincts aswell, guess I'll have to look for an alternative.
 

monday

Expert
Joined
Jun 23, 2014
Messages
1,127
Solutions
1
Reaction score
158
but what's wrong about the current solution? Just dont call "free" unless you don't need tthese strings/memory anymore, and in such case read the sampfuncs global variable and call free using its' value
 

Zin

Expert
Joined
Aug 1, 2013
Messages
1,734
Solutions
2
Reaction score
117
but what's wrong about the current solution? Just dont call "free" unless you don't need tthese strings/memory anymore, and in such case read the sampfuncs global variable and call free using its' value
But If I want to use 31@ for something else I need to clear it though right?
 

monday

Expert
Joined
Jun 23, 2014
Messages
1,127
Solutions
1
Reaction score
158
as long as you keep the reference to the array, you can use 31@ for other purposes

It's not like 31@ itself at any time stores the whole array somehow. It just holds the pointer to it, a single integer. So you can make a copy of that integer (which you do) and use 31@ for other things.

You could google "how malloc/free works"

You could also check these vids:
 
Status
Not open for further replies.
Top