Hello, I am investigating in depth the operation of one of the codes made by Nick Gammon, but I came across a variable that I had never seen, I cant find any explanation about the use of it on the internet. The variable is "register".
If it's not a bother, I would be grateful if someone explained to me what the "register" variable is for and what benefits it gives to using it (I also saw that it can be declared as "const register").
Please direct us to the variable in question. The link you posted has pages and pages of stuff to go through. Make it easier for us by at least quoting the section where it is used, and explain how it is used...
The 'register' keyword is a hint to the compiler that you would like the variable to be kept in a register rather than having it fetch from and save to memory.
I did not understand, I think I had already heard that part that is a suggestion for the compiler. Sorry for what I'm going to say (I'm not an Arduino expert), as I remember there are like 32 registers inside the AVR, what you tell me is that by putting "register" to another variable, it allows me to access it more quickly ?
hiperdoo:
Hello, I am investigating in depth the operation of one of the codes made by Nick Gammon, but I came across a variable that I had never seen, I cant find any explanation about the use of it on the internet. The variable is "register".
In your later quoted code, you'll notice that "register" appears twice, thus "register" cannot be a variable name.
The variables are "linePointer" and "messagePointer".
It's a suggestion to the compiler that you'd like the fastest possible access to those variables.
The compiler can choose to ignore your suggestion.
With the information you said, I came to understand that "register" allows me to access variables more quickly but there is also some chance that the compiler will ignore this suggestion...
This raises two more questions for me:
What is the difference in clock cycles between using and not using "register".
What circumstances make the compiler ignore my "register" request?
From what I can find, register was deprecated in C++11, and removed in C++17 (but reserved as a keyword for possible future use), so should make no difference at all in the code produced by the compiler.
I doubt that this is the case, this code was created in 2012, at that time it must have had some use, also, this is an optimized code, so it must have a specific use.
What is the difference in clock cycles between using and not using "register".
What circumstances make the compiler ignore my "register" request?
It depends on the size of the variable and the machine architecture.
When there aren't any available registers. (Or a bunch of other reasons).
Your request for locking a variable into a register could spoil the compiler's attempts at optimisation.
Standard compiler optimizations will put things in registers as much as possible, probably doing a better job than you would at picking which variables will yield the most improvement. With simple programs (like most Arduino sketches) and modern CPUs (with a significant number of registers), probably ALL simple local variables end up in registers. (the PDP-11 on which C was originally written had six registers...)
Well, I need the exact details of the operation of "register", someone know in which page of the official manual of ATmega328 this information is, or of some web page?
hiperdoo:
Well, I need the exact details of the operation of "register", someone know in which page of the official manual of ATmega328 this information is, or of some web page?
I think it would be more relevant to refer to the compiler's ABI.
But, as has already been pointed-out, you're flogging a dead horse, or at least one that's about to expire soon
.
hiperdoo:
Well, I need the exact details of the operation of "register", someone know in which page of the official manual of ATmega328 this information is, or of some web page?