Sie sind auf Seite 1von 6

REGISTER STORAGE CLASS IN C - part 1 By : - GAUTAM KUMAR SINGH NIE (1) when & why register storage class

should be used ? (2) How many register variables can be declared ? (3) why register storage class have been provided in c ? whatever functionality is provided by register storage class could it have been provided without the same? (4) As register allocation is done for auto variables can it be done for global variables ? if yes how, otherwise why it is not allowed ? what kind of variables can be given register allocation ? (5) can register storage class be used for all kind of data types ? (6) Can we access address of register variables ? what if compiler deny to store variable in registe r & store it in memory, if not why not ? what if same question is repeated for c++ ? (7) Features of register storage class? (8) Disadvantage of register storage class? (9) Initialisation of register variables? (10) Should register variables be used with modern day compilers ? what about conventional compilers? , MYSORE , ISE

ANSWERS: (1) Register storage class can be useful When you want to refer a variable frequently & when you think its necessary to put the variables into the computer registers You can apply register specifier to variables .you can ( not always compiler have authority) allocate fast memory in the form of a register to that variable. e.g. For variables such as loop counters, register allocation may be useful . Each computer (processor) has a certain number of registers to hold temporary data and perform calculations. The access time of the register is much less than main memory since extra CPU cycle is required to access data from main memory .Therefore, storing variables in registers might help to speed up program execution. Thus, register variables provide a certain control over efficiency of program execution.

(2) Total number and types of variables actually allocated memory in register may vary from machine to machine ( since different machine may have different no , size of registers & also different specific purpose of some registers ) and compiler to compiler since the processor has a

limited number of registers .The register specifier only gives the compiler a suggestion (acts as a directive).In other words, it does not guarantee the allocation of a register for storing value of that variable .The compiler can ignore the suggestion if there is no register available, or if some other restrictions ( like variable can't fit in) apply and treat variables as if they were normal automatic variables without issuing any error message . we need not to worry about declaring too many register variables because the compiler automatically transforms register variables into automatic variables when the limit is reached.

(3) Register specifier had been provided to give programmers some control over efficiency of program execution taking advantage of memory hierarchy in the system .The keyword register gave the compiler-writer a clue about what variables the programmer thought were frequently referenced, and hence could usefully be kept in registers . Having a register keyword simplifies the compiler design by transferring this burden to the programmer. ( Thats what have been also done for printf(), scanf() functions to provide them clue in form of format specifiers abour number & type of variables to be dealt with .many more such examples can be given .). yes it can be done without using register keyword , modern optimizing compilers does the same thing without any hint from the programmer rather we can say it does better job by allocating registers for individual uses of a variable, rather than reserving them for its entire lifetime at declaration.

(4) You cannot use register allocation for global variables because memory is allocated to the global variable at the beginning of the program execution. At that time, it is not certain which function is invoked and which register is used. Function code may use the register internally, but it also has access to a global variable , which might also use the same register. This leads to contradiction, so global register variables are not allowed. You can only apply the register specifier to local variables and to the formal parameters in a function . The register specifier also can be used only with variables of block scope. It puts a variable into the register storage class, which amounts to a request that the variable be stored in a register for faster access. It also prevents you from taking the address of the variable.

(5) The types of variables that can be assigned to registers vary among machines. Generally the basic data types (int , char) can usually be assigned to registers, as well as pointers to any data type because size of registers to hold no of bytes required for storage of data type may be sufficient to hold these data types only. if the processor has special register (as for most pentium machines ) that can hold float & double type variables & if such register are sufficient in number such that some of those registers may be free at any instant of time & compiler have allowed then definitely register storage clas can be used for these data types also . if we use register storage class for a data type which require large space than register we dont get any error message compiler would treat those variable to be of auto storage class.Therefore ,in

practice, Larger objects, such as arrays, structure , union etc obviously cannot be stored in a register.

(6) In C , Its illegal to take the address of a variable that is declared with the register specifier by using the unary & operator either explicitly or implicitly, because a register variable is intended to be stored in a register of the CPU, not in memory. A CPU register does not have a memory address usually that you can access. This restriction applies even when the compiler ignores the request and puts the variable in addressable memory. it has been done to simplify compiler design . Other than that, register variables behave just as ordinary automatic variables . C++ lets you take the address of an object with the register storage class. For example: register int x; int* y = &x; For example: If you try to access address of a variable declared to be of register storage class we get compilation error.Its true even though request for allocation of register have been denied by the compiler. (i) #include<stdio.h> void main() { register int x=6; int *ptr; ptr=&x; printf("address of register variable x= %u\n",ptr); } Output: Compilation error (ii) we get error message also when we want to access address of register variable in scanf() statement. #include<stdio.h> void main() { // valid in C++, but invalid in C ( you can think how it would have been implemented )

register int x; printf("\n enter value of register variable x\n"); scanf("%d",&x); printf(" value of register variable x=%d\n",x); } Output: Compilation error (iii) it seems from above program that we can not enter value through keyboard for a variable of register storage class. But we can do it alternatively as in program below. #include<stdio.h> void main() { register int i; int j; printf("enter value for register variable\n"); scanf("%d",&j); i=j; printf("\n value of register variable i=%d \n",i); }

(7) Properties of variable defined to have register storage class in c prorgramming language

Storage

- Cpu register if allocated otherwise in main memory

Default initial value - garbage /unpredicted Scope - Local to the block in which the variable is defined.

Life

- till control remains in the block in which variable is defined

(8) A limitation of register variables is that you cannot generate pointers to them ( i.e can't take address of a register variable ) using the & operator (This is because, on most machines, pointers are implemented as memory addresses, and CPU registers usually do not have memory addresses.), a register is allocated for the variable if granted for entire lifetime of variable this is also a disadvantage . But the 'register' storage class doesn't provide much of an advantage when it is used: the compiler is permitted to ignore it when selecting placement of variables, compiler optimization of generated code may do a better job of optimizing placement than the programmer can, and the language imposes restrictions on the use of variables defined as register .

(9) You can initialize any register object except parameters. If you do not initialize an register object, its value is garbage. If you provide an initial value, the expression representing the initial value can be any valid C expression. Formal arguments in a function definition may have class register, in which case they are copied from the calling stack into a register, where possible. NOTE :- The register class is the only storage class that can be explicitly specified for function parameters.

(10) Explicit register declarations should not be used now a days, because modern compilers generally do an excellent (better than most of programmers does optimization) all by themselves and without any hints, of deciding which variables should be kept in machine registers & uses sophisticated register allocation techniques that make the use of the register keyword unnecessary .In fact, such compilers usually ignore register allocation part of the register keyword semantics and only retain the "no unary & operator" semantics .However, for portability all other semantics associated with the register keyword are honoured by the compiler. hence better style should be not to use register storage class with such compilers unless it is desired to loose / not required the fascility of accessing variable through pointer ( if any ) . i.e. The register storage class specifier keyword does not influence the optimization and performance of generated code for modern day compilers It only prohibits (for C code) the unary prefix addess-of operator & register keyword be used on arrays , or if any other. hence in future C & C++ standards the register keyword might change its meaning or may no longer be used. probably it have not been done for the sake of portability. The compiler should use as few registers in the register set as possible( if u are interested can think of such algorithms) if the number of registers needed is significantly greater than the number of

registers available, each of these register allocation methods generates a large number of , the loads and stores to memory generated by the register allocator.The compiler should assign temporaries to registers so that the number of LOAD and STORE instructions inserted by the register allocator are as small as possible during the execution of the program.hence to a good optimising compiler, forcing a variable to be kept in a register for the whole duration of a function's execution is usually a disadvantage because register allcator can decide better when keeping that variable in a register makes sense.hence,forced allocation of register variables could actually result in slower execution. Even the availability of register storage does not guarantee faster execution of the program. For example, if too many register variables are declared, or there are not enough registers available to store all of them, values in some registers would have to be moved to temporary storage in memory in order to clear those registers for other variables. Thus, much time may be wasted in moving data back and forth between registers and memory locations. Also, the use of registers for variable storage may interfere with other uses of registers by the compiler, such as storage of temporary values in expression evaluation. hence register variables can be used better if you have a detailed knowledge of the architecture and compiler for the computer you are using.

Register storage class was useful for useful for old day compilers . In 1972 when c was invented there was not too much difference between accessing time of register & main memory but still Dennis ritchie implemented functionality of register storage class to make C language what it is, what a genius man , its mental effort of dennis ritchie that C is still so much powerful & useful even after so much change in hardware technology ( like now a days there is so much difference in accessing time of register & main memory ).

Das könnte Ihnen auch gefallen