Register variables are subset of local variables which are managed directly in registers of central processing unit / CPU. Variables are managed in CPU registers so this type of variable does not have any memory location. Address of these variable can not be assigned to any pointer variable. CPU registers are located next to ALU and CU unit of the CPU's core unit. CPU never need to perform any memory and cache operations. Reading and writing register variables are the fastest. Register variables live in function scope or local scope which is same as the case of auto variables.
Declaration of register variable
Register variable will be utilizing CPUs general purpose internal registers. It strictly depends on the CPU architecture and the availability of the registers. One or two register variable are possible in one context of time. Let us see CPU registers of two most used CPU architectures.
x86 CPU registers
ARM CPU registers
Automatic variable are declared inside local function scope and stored in stack memory which makes access to these variables much slower than registers. However there are cache memory to do optimization in CPU level but cache memory is not as fast as CPU registers.
Auto variables will be located in the stack memory segment. Most of the CPU architectures manage stack growth downwards in the memory. Stack pointer will be assigned to the top of stack segment area. It will grow downwards as program makes more inner function calls and function returns will move upwards. Now C compiler uses right to left argument pushing. So a function with two arguments will be pushed from right to left. Here we have a function "function(x,y)" will make a push y and then push x. Then return address of the function, BP will be pushed. Local variables will be located below of these.
CPU, Cache and main memory
CPU will be loading auto variables to the registers and arithmetic or logical operations will be performed. After that it will be written back to memory. Loading variable from memory and storing variable to memory again will require memory bus operations. These are slow and every time bus operations slows down the system.
Cache memory plays a major role here. Cache memory is a high speed static RAM memory and located very near to CPU's core. CPU reads variable only once from main memory and then it will be stored in cache memory. Any arithmetic or logical results will be stored in cache memory temporary basis. Final result will be stored in memory. CPU and cache logics will maintain coherency and optimisation so that multiple times memory bus read and writes will be reduced. Data cache /D-Cache in L1 Cache memory will be used. I-Cache or Instruction cache is used by CPU to optimize instruction execution.
Cache in Multicore & muti-socket
Cache memory organization in multicore and multisockt systems are more complex. Here each CPU core will have L1 cache. A Multicore CPU chip will have multiple CPU cores with their individual L1 caches. Now to maintain cache coherency L2 cache will be there. L2 cache will maintain coherency and consistency between all the CPU cores. Now coming to mutisocket systems. Here we have two CPU sockets and they have individual L2 caches. There will be a L3 cache memory to maintain coherency. If we consider speed of operations we get this levels: CPU registers > L1 Cache > L2 Cache >L3 cache >Main Memory.
Declaration of Auto variable
Auto variable can be declared with the keyword identifier "auto". C compiler defaults all variable to auto if no identifier is supplied by developer.
Auto variables can be converted to register variables by putting the register identifier before it. It has platform specific limitations. Register variables will work only if free registers are available to hold the variable for a function scope. In case of Microprocessors or micro-controllers having very less number of general purpose registers, register variables will never be taken even if declared as register.
Auto variable vs Register veriable
|Identifier: auto||Identifier: register|
|Access time is slower than register||Access time is fastest|
|Storage is in memory/cache||Storage is in CPU register|
|Variable will have memory address||Not memory addressable|
|Large number of variables can be created||Very few variable can be created|
About our authors: Team EQA
You have viewed 1 page out of 248. Your C learning is 0.00% complete. Login to check your learning progress.
Questions index C Questions C++ Questions Win32 MFC COM/DCOM DLL Questions
Compilers & Editors
Download Visual Studio Download XCode Download Visual Studio Code Android studio install sdk Eclipse installer Best C compilers IDEs
Development system setup
Windows media creation tool MSDN subscription Ubuntu virtualbox
New updated posts
Why learn C? Calculate weighted average