NEWTON
Asked
6 months ago
3
views
0
Since memory cannot be freed, what's the drawback of only using local variables (to avoid revoked refs)? What's the typical use case for tempvars?
ㅤ This question was originally posted on Triality
0
From what I have seen, locals are safer to use than tempvars because you will not get your reference revoked as you mentioned above. In most cases you will have the same result using either (I am curious as to the language creators intention). But here is a simple example that could illustrate a slight difference. Take the following to code snippets
func main{output_ptr : felt*}():
tempvar x = 1
return ()
end
and
func main{output_ptr : felt*}():
alloc_locals
local x = 1
return ()
end
The first code snippet will execute in 3 steps and the second will operate in 4. So I think at least in simple cases for functions you could save operations.
ㅤ This answer was originally posted on Triality
answered
7 months ago
0
One use case of tempvar
is with the new
operator. You have to use a tempvar otherwise you'll get a The use of 'new' in reference expressions is not allowed
error. See: https://cairo-lang.org/docs/how_cairo_works/object_allocation.html?#the-new-operator
ㅤ This answer was originally posted on Triality
answered
7 months ago
Cairo: What is the difference between let and tempvar?
Cairo Lang / StarkNet: What are Revoked references? What is alloc_locals?
Cairo Error calling function: Expected 'function_name' to be a struct. Found: 'function'.
What does ret do in Cairo?
Namespaces in Cairo
Are there situations in which we prefer declaring variables as `local` vs `tempvar`?
Cairo Error: Unexpected token Token('LPAR', '('). Expected: identifier.
Braavos does not seem be injected when running app locally it does work when deployed is there something i should check?
How can I interact with my contract on Starknet using starknetjs?
Cairo Owner Only Fuctions
Do you know a function that converts an array of felts (numbers) to a single string in JS? [Cairo Lang]
Cairo Testing Error
Is there a way to find events in a block without getting receipt for all transactions on Starknet?
How do you optimize gas in Cairo with Uint256/felt?