CLEO Help [SOLVED] Create a linked list

CLEO related
Status
Not open for further replies.

JaggerJam

Active member
Joined
Sep 14, 2017
Messages
98
Solutions
1
Reaction score
21
Hello, is a way to create a linked list in CLEO, like this.
I know how it works, but in CLEO idk how to reproduce, here is a.k.a pseudocode

Code:
{$CLEO}
0000:

repeat
wait 0
until SAMP.Available()


0b34: "list" @linked_list

while true
wait 0
 
    if key_press 190 // period or >
    then
        // store the next string
        chatmsg "%s" -1 // next string from list
    end
 
    if key_press 188 // comma or <
    then
        // store the prev string
        chatmsg "%s" -1 // prev string from list
    end
 
END


:linked_list
1@ += 1
alloc 0@ 50
format 0@ "string%d" 1@
//insert in a list
free 0@
SAMP.CmdRet()
 
Last edited:

JaggerJam

Active member
Joined
Sep 14, 2017
Messages
98
Solutions
1
Reaction score
21
So I have came up with this, but it gives me wrong strings and also trying to define the null pointer but no success
Code:
0b34: "list" @linked_list

alloc 0@ 128
0C11: memset destination 0@ value 0 size 128
0085: 31@ = 0@
1@ = 0

while true
wait 0
    
    if key_press 190 // period or >
    then
        4@ = 0
        0A8D:  3@ = read_memory 0@ size 4 virtual_protect 0   
        0C17: 4@ = strlen 3@
    
        0@ += 4
        if not 4@ == 0
        then
            chatmsg "Next: %s | %d" -1 3@ 4@
        else
            0@ -= 4
        end
    end
    
    if key_press 188 // comma or <
    then
        4@ = 0
        0@ -= 4
        0A8D:  3@ = read_memory 0@ size 4 virtual_protect 0
        0C17: 4@ = strlen 3@ //try to detect the null pointer, no succes
        
        if not 4@ == 0
        then
            chatmsg "Prev: %s | %d" -1 3@ 4@
        end
        
    end
    
END


:linked_list
1@ += 1
alloc 2@ 50
log "Inserted string%d" 1@
format 2@ "string%d" 1@   
0A8C: write_memory 31@ size 4 value 2@ virtual_protect 0
31@ += 4
free 2@
SAMP.CmdRet()

 

JaggerJam

Active member
Joined
Sep 14, 2017
Messages
98
Solutions
1
Reaction score
21
Solved, mb.. in primitive way, but it's working. Also found the write_opcode needs a delay, without the delay "list" get messy
Code:
{$CLEO}
{$INCLUDE SF}
0000:

repeat
wait 0
until SAMP.Available()

0b34: "list" @linked_list

alloc 0@ 128
0C11: memset destination 0@ value 0 size 128
0085: 31@ = 0@
1@ = 0

while true
wait 0
    
    if key_press 190 // period or >
    then
        if not 4@ == 0       
        then
            0A8D:  3@ = read_memory 0@ size 4 virtual_protect 0       
            0@ += 4
            chatmsg "Next: %s | %d" -1 3@ 4@
            4@ -= 1
        end
    end
    
    if key_press 188 // comma or <
    then
        4@ += 1
        if 801d: not 4@ > 1@
        then
            0@ -= 4
            0A8D:  3@ = read_memory 0@ size 4 virtual_protect 0
            chatmsg "Prev: %s | %d" -1 3@ 4@
        else
            4@ -= 1
        end
        
    end
    
END


:linked_list
1@ += 1
alloc 2@ 50
log "Inserted string%d" 1@
format 2@ "string%d" 1@   
0A8C: write_memory 31@ size 4 value 2@ virtual_protect 0
31@ += 4
free 2@
0085: 4@ = 1@
SAMP.CmdRet()


Thx for help. jk
 

monday

Expert
Joined
Jun 23, 2014
Messages
1,127
Solutions
1
Reaction score
158
Notice that you're using free 2@, which means that 31@ (or 0@) holds addresses to memory that is not allocated anymore. It appears to work because that memory wasn't cleaned-up or replaced with some other data, but at some point it will be, and the code will be likely to misbehave/crash.

At the begining of the code you could initialize the whole array 0@ with 0's. Then whenever you want to put some string in the array use:
Code:
if value_at_location == 0
then
    alloc location max_size
end

format value_at_location "mystring"

This way you don't have to worry about freeing memory
 
Status
Not open for further replies.
Top