NEWTON
Asked
5 months ago
43
views
1
I am currently running this code
func main() {
let x = [ap];
[ap] = 1, ap++;
[ap] = 2, ap++;
[ap] = x, ap++;
jmp rel -1; // Jump to the previous instruction.
}
And what is happening is that "x" is alternating between 1 and 2 and I couldn't figure out why.
Also, it wasn't very clear to me what a "Revoked Reference" means. Is it when you cannot access a certain [ap] when running a specific function?
This is the link to the exercise:
https://www.cairo-lang.org/docs/how_cairo_works/consts.html#revoked-references
1
Accepted answer
That's actually a nice question! In your example, the compiler is not able to compute the change of ap, and here's why "x" is alternating between 1 and 2
You can see a similar question there it is explained a bit more clearly explained what is a revoked reference.
The scope of some types of references in Cairo (e.g., return values of functions and temporary variables) is somewhat restricted. For example, a call to another function may revoke those references.
When you write "let" you do not create a variable, instead you create a reference to the memory slot. I may even cause an error, for example:
let (x) = foo(1); let (y) = foo(2); serialize_word(x + y);
will cause an error:
Error: code:43:20: Reference 'x' was revoked. serialize_word(x + y); ^ Reference was defined here: code:41:10 let (x) = foo(1); ^
vargastartup
answered
5 months ago
how I could read a felt* from ap in Cairo lang?
Cairo Lang / StarkNet: What are Revoked references? What is alloc_locals?
What is the inefficiency in this cairo code using alloc_locals
Why does this Cairo program put powers of 2 in the memory?
a pointer with a hint, without doing a memory allocation
How to use get_fp_and_pc in Cairo Lang?
What is the difference between tempvar/let in Cairo Lang? How to use allow_locals and local?
How can I send a Uint256 amount of ERC20 tokens from L1 to starknet? And how should I build my payload for "sendMessageToL2" to match the Uint256 format of Cairo?
Warp Traspiler error: the warp-related package was not found. How do I generate python packages?
how I could read a felt* from ap in Cairo lang?
How do I connect my DAPP to a Starknet node using starknetjs?
ApeWorX: How can I test on multi-chain with both Starknet and Ethereum?
Does Cairo work in python3.10 venv? I can’t install python 3.9
Cairo OR operator