CLEO Help Alloc + 0AD3: + 0AD2: + 0AD1: // Memory transfert Question

CLEO related
Status
Not open for further replies.

Grubitsh

Active member
Joined
Jul 28, 2014
Messages
104
Reaction score
0
Hello guys,

I am a bit lost about what I am coding.
I got multiple render.Drawtext() in my main, and the strings they show are updated by a 0AD1: function.
I got some crashes and I think it's about memory problem.

I don't really know how to handle my memory in this case. In fact, I would like to know, if you alloc memory into a function 0AD1: then does the compiler understand that the returned value is alloced 260 then will automaticaly alloc the same ammount of memory for the variable in the main ? Like
Code:
0@ = load font verdana blablablablabla + starting stuff
While true
wait 0

// Conditions here
0AD1: @An_Amazing_Function 0 1@
// End of condition
// Do things here
//Conditions here
render.drawtext(0@, 1@, 10, 210, -1)
//End

end

:An_Amazing_Function
 // Conditions + Do things here
alloc 25@ = 260
0AD3: 25@ = format "%s" 11@
0AD2: ret 1 25@

  • Will the compiler automaticaly alloc 1@ = 260 because it's a returned value that has already been allocated in the 0AB1: function?
  • Will the compiler automaticaly "free 25@" at the end of the function ?
  • Should I alloc 1@ = 260 at the begining of the script to avoid crashes ?
  • What happens if I alloc 260 a lot of variable (even if it's useless) and never "free" them ?
 
Joined
Feb 18, 2005
Messages
2,965
Reaction score
272
Maybe you crash because 0AD1/0AD2 are not the same with 0AB1/0AB2.

Code:
Will the compiler automaticaly alloc 1@ = 260 because it's a returned value that has already been allocated in the 0AB1: function?

You can't expand variables memory, 1@ will always be able to store only 4 bytes.
What alloc(0A8C) does is to allocate a memory block and return you the pointer to that newly reserved space. Since the pointer is just an address you return the address of the pointer and not the memory block.

Take this example;

alloc 0@ = 260 //CLEO calls malloc which allocates memory somewhere in the gta heap memory.
//now 0@ contains something like 0x691337, this address points to the allocated memory

format 0@ = "hello this is a test"  //fill that memory with something
0AD1: "%p" 1000 0@ //this will show you the address of the pointer to the newly allocated memory
wait 1000 
0084: 5@ = 0@ //now 5@ contains the address of the same pointer
0AD1: "%s" 1000 5@ //so 5@ will be "hello this is a test" even though we didn't called 'alloc 5@ = 260'

So every variable can now have access to that allocated space if you assign the value of 0@ to any other variable.
You can use '0AC7: 1@ = var 0@ offset' to get the variable address in memory.

Code:
Will the compiler automaticaly "free 25@" at the end of the function ?

No, it's up to you to free allocated memory. CLEO will free stuff itself when it unloads(normally on game exit).

Code:
Should I alloc 1@ = 260 at the begining of the script to avoid crashes ?

It won't avoid crashes, like i said above 1@ contains a pointer, so if you go;

alloc 1@ = 260
1@ = 100 //1@ doesn't hold the pointer address anymore
format 1@ "long text here" //crash

Code:
What happens if I alloc 260 a lot of variable (even if it's useless) and never "free" them ?

Nothing should actually happen'.. not really.. maybe.. it's classified  :trollface:
alloc 0@ = 260 //creats a new block of memory at address 0x100500
alloc 0@ = 260 //creats a new block of memory at address 0x220650, but doesn't free previous memory.
Correct way should be to allocated and free when done.

#endshittyexplanation

Now let's think of your example, in CLEO you can't do two things at the same time right?
So best way would be to reuse the same variable and memory pointer.

Code:
alloc 0@ = 260 //we do this only once

while true
wait 0
    if 0ab0: 49
    then 
        call @test1 1 0@ //send in the pointer to 0@
        0AF8: "%s" -1 0@
        call @test2 1 0@
        0AF8: "%s" -1 0@
    end
end

:test1
0209: 2@ = random_int_in_ranges 69 1337
format 0@ "this is a testttt %d" 2@ //modify memory directly
ret 0 //we don't have to return anything

:test2
format 0@ "ayy lmaoo wtf is dis" 
ret 0

So what i'm saying to do in relation to your code:
call @FunctionThatModifyString 1 0@
render.drawtext 0@
call @FunctionThatModifyString 1 0@
render.drawtext 0@
...
 

Grubitsh

Active member
Joined
Jul 28, 2014
Messages
104
Reaction score
0
Yes lol, I failed during writing this post, this is not 0AD1: 0AD2: xD, this is 0AB1: and 0AB2: but you understood it so it's ok.

Well thanks for you help, but I still have a crutial questions about this part of your exemple

Code:
call @test1 1 0@ 

:test1
0209: 2@ = random_int_in_ranges 69 1337
format 0@ "this is a testttt %d" 2@ //modify memory directly
ret 0 //we don't have to return anything

The CRUTIAL QUESTION
What I understood about 0AB1: (or Call) is that you can do everything with your variables, it won't change anything in the main unless you return the value. (so it saves a lot of variables because we are stuck with 32 variables in total)
Like

Code:
1@ = 5
2@ = 7
call @ThisFunction 2 1@ 2@ // input 1@ and 2@ but return nothing so 1@ and 2@ shouldn't change whatever happen in the function
0AF8: "%d" -1 1@ // will show 5
0AF8: "%d" -1 2@ // will show 7

:ThisFunction
1@ += 152454
2@ -= 1561651
0AB2: ret 0

In your exemple below, you tell me that you can change a input value without returning it.
So what did I miss about the 0AB1 understanding ? (ALL  ? :computer_guy: )
 
Joined
Feb 18, 2005
Messages
2,965
Reaction score
272
When you allocate memory using 0A8C it returns a value, that value is actually the memory address of a pointer, this pointer points at the block of memory allocated.

Say we do 0A8C: 0@ = 260
Now 0@ value is 0x123456.

call @test1 1 0@
call @test1 1 0x123456

both these calls will have the same effect, we change the memory pointed at by that address not what is stored in the variable.
 
Status
Not open for further replies.
Top