
- Forum
- Programming Talk
- C and C++
- Want to Know the Reason
Want to Know the Reason
This is a discussion on Want to Know the Reason within the C and C++ forums, part of the Programming Talk category; I declared an auto variable x and print it loping through 1 to 1000 times in my program 1. In ...
-
05-29-2007, 10:35 AM #1
- Join Date
- Apr 2006
- Answers
- 124
Want to Know the Reason
I declared an auto variable x and print it loping through 1 to 1000 times in my program 1. In my program2 I declared a register variable x and print it loping through 1 to 1000 times. Though both gives the same output from 1 to 1000 I want to know which works faster and which is better in performance. Kindly explain the reason for the same.
-
Reason
Hi,
Since an auto variable is stored in memory and a register variable is stored in a CPU Register in the hopes of enhancing performance by minimizing access time. . Values stored in a register is executed faster than one stored in memory. However, the compiler is not required to honor this request. Because of the limited size and number of registers available on most systems, few variables can actually be put in registers. If the compiler does not allocate a machine register for a register object, the object is treated as having the storage class specifier auto.
Register access is faster than memory access. The register storage class is a hint to the compiler that the variable will be heavily used. Storing data there gets rid of the overhead of retrieving the data from normal memory. This memory is quite small compared to normal memory though so only a few variables can be stored there.
Point to be noted:
* A register does not have an address and & operator cannot be used/applied
* One cannot use a pointer to a register storage class specifier
I hope i have cleared your doubt. For further clarifications you can mail back.
Gayatri
-
12-27-2010, 07:12 AM #3
- Join Date
- Dec 2010
- Answers
- 32
hello.....
for this particular case, register is the answer as because register keyword will store data in the register which is situated in the processor of the cpu. so the data tranfer between the cpu and ALu will be much faster than the data transfer between ram and alu.....
-
Sponsored Ads

Reply With Quote





