Http://blog.sina.com.cn/s/blog_6a1837e90101128k.html
Register: ThisThe keyword request compiler tries its best to store variables in the internal registers of the CPU, instead of accessing the variables through memory addressing to improve efficiency..Note that it is not absolute. Think about it, there are just a few or dozens of registers for a CPU. If you define many register variables, it may not be able to put all these variables into registers if it is exhausted, the wheel may not reach you.
I. Little eunuch around the Emperor ---- register
I don't know what registers are? Have you ever seen eunuch? No? Actually, I do not. It doesn't matter if you haven't seen it. It's a big headache if you have seen it. ^ _ ^ When the Emperors want to read the playing stamp, the minister always handed over the playing stamp to the eunuch next to the emperor, and then the eunuch handed over to the emperor for handling. This little eunuch is justTransfer Station.
Well, we think of our CPU. Isn't CPU our comrade emperor? The Minister is equivalent to our memory, and the data is taken out from him. That little eunuch is our register (here we do not consider the high-speed cache area of the CPU ). The data is taken out from the memory and put in the register first, and then the CPU reads the data from the Register for processing. After processing, the data is also stored in the memory through the Register,CPU does not directly deal with memory. Here, it should be noted that little eunuch took the playing stamp from the minister's hand and handed it over to the Emperor's comrades on his own initiative. But the register was not so conscious and he did not take the initiative to do anything. An emperor may have some eunuchs, so a CPU
There can also be many registers, with different types of CPUs having different registers.
Why is it so troublesome?Speed! Because of the speed. A register is actually a small storage space, but its access speed is much faster than the memory. The first month of the entrance is the first month. It is very close to the CPU, and the CPU gets the data as soon as it reaches out. Is it faster than searching for data on a specific address in such a large memory? Someone asked us that since it is so fast, our memory hard disks have all been changed to registers. What I want to say is: you really have money!
Ii. Example
Register modifier implies CompilationProgramThe corresponding variables will be frequently used. If possible, they should be stored in the CPU registers to speed up their storage.For example, the following memory block copyCode,
# Ifdef nostructassign
Memcpy (D, S, L)
{
Register char * D;
Register char * s;
Register int I;
While (I --)
* D ++ = * s ++;
}
# Endif
Iii. Notes for using the register Modifier
However, the Register modifier has several restrictions.
First,RegisterThe variable must be a type that can be accepted by the CPU.. This usually means the register variableMust be a single valueAndThe length must be less than or equal to the length of an integer..However, some machine registers can also store floating point numbers.
Second, because the register variable may not be stored in the memoryYou cannot use "&" to obtain the address of the register variable..
Due to the limited number of registers, and some registers can only accept specific types of data (such as pointers and floating-point numbers ), therefore, the real number and type of the register modifier depend on the machine where the program runs, and any redundant register modifier will be ignored by the compiled program.
In some cases, saving variables in registers reduces the running speed of the program. BecauseOccupied registers cannot be used for other purposes; OrThe number of times variables are used is insufficient, which is insufficient for loading and storing the additional overhead of variables.
Early C compilation programs do not store variables in registers unless you command them to do so. At this time, the register modifier is a valuable supplement to the C language. However,With the development of compilation programming technology, the current C compiling environment can make better decisions when deciding which variables should be stored in registers.. In fact, many compilers ignore the register modifier because although it is completely legalIt onlyIt's a suggestion, not a command..