The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Float variable in register storage class
Discuss Float variable in register storage class in the C Programming forum on Dev Shed. Float variable in register storage class C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

August 5th, 2011, 01:34 AM
|
|
Registered User
|
|
Join Date: Aug 2011
Posts: 1
Time spent in forums: 12 m 59 sec
Reputation Power: 0
|
|
|
Float variable in register storage class
Hi
i ran the following code on QNX platform using gcc compiler:
register float value;
value =1.2;
printf("Value= %f",value);
Console output: 1.2
I read somewhere that if the above snippet is run, the integer part of the float variable is returned (due to the register storage class) but isn't true in the above case.
How is a float variable stored inside the memory in case of the register storage class?
Thanx
|

August 5th, 2011, 04:35 AM
|
 |
Contributed User
|
|
|
|
Quote: | Originally Posted by C99 draft
A declaration of an identifier for an object with storage-class specifier register
suggests that access to the object be as fast as possible. The extent to which such
suggestions are effective is implementation-defined.
...
The implementation may treat any register declaration simply as an auto declaration. However,
whether or not addressable storage is actually used, the address of any part of an object declared with
storage-class specifier register cannot be computed, either explicitly (by use of the unary &
operator as discussed in 6.5.3.2) or implicitly (by converting an array name to a pointer as discussed in
6.3.2.1). Thus, the only operator that can be applied to an array declared with storage-class specifier
register is sizeof.
|
You can declare all sorts of things as being 'register', but that doesn't actually mean a great deal (the compiler is free to ignore it).
The only consistent and provable thing that will happen though is that it prevents you from creating pointers to things declared register.
|

August 5th, 2011, 05:28 AM
|
|
|
Quote: | Originally Posted by tarun27sh
I read somewhere that if the above snippet is run, the integer part of the float variable is returned (due to the register storage class) but isn't true in the above case.
|
Any compiler that did such a thing would be a faulty compiler.
Quote: | Originally Posted by tarun27sh
How is a float variable stored inside the memory in case of the register storage class? |
However the compiler chooses. All the register keyword does is provide a hint to the compiler. The compiler is perfectly free to ignore that hint.
__________________
Right 98% of the time, and don't care about the other 3%.
“It has been said that the great scientific disciplines are examples of giants standing on the shoulders of other giants. It has also been said that the software industry is an example of midgets standing on the toes of other midgets.” (Alan Cooper)
|

August 5th, 2011, 02:33 PM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
|
In most cases on most architectures the compiler will ignore it (I've never come across one that didn't). The compiler knows better how to optimise code for speed than you do, such manual hints are unnecessary and doomed to have little or no effect. Consult the compiler documentation for what it actually does.
GCC does at least allow you to force the use of specific registers (http://gcc.gnu.org/onlinedocs/gcc/Explicit-Reg-Vars.html#index-explicit-register-variables-2673), but in normal ISO C usage, I believe ignores it.
Most processors have precious few general purpose registers (especially x86), hence the directive is ignored. The ARM ABI uses the first four GP registers to pass parameters.
For a floating point variable, register storage (whether directed or not) may be affected by the presence or otherwise of an FPU.
A single precision floating-point value is 32 bits, on a 32 bit processor it will fit entirely in a register. FPU registers are larger if the FPU supports double precision.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|