register char str[LEN] 
Author Message
 register char str[LEN]

It seems that when I declare a char array as register, the compiler (gcc)
returns the warning: address of register variable `str' requested

What can be wrong? Is it inherently wrong to declare a char array as a
register variable?



Thu, 30 Jan 2003 03:00:00 GMT  
 register char str[LEN]

Quote:

> It seems that when I declare a char array as register, the compiler (gcc)
> returns the warning: address of register variable `str' requested

> What can be wrong? Is it inherently wrong to declare a char array as a
> register variable?

A char array requires one byte of storage per character. The general
purpose registers on most machines have a small, fixed size, typically 4
or 8 bytes. This limits the size of an array that can be stored in a
register.

Arrays are generally element addressable, but, in many machines,
registers are not. Many array operations are described in terms of the
address of the first element, but, in many machines, registers do not
have an address.

Therefore, the standard says that the only operator you can apply to an
array of storage class register is sizeof.

So it's legal and theres nothing wrong with it, but it's pretty useless.

Today's compilers do a pretty good job of using registers where they are
called for. It's best to avoid storage class register until you have
detected a particular instance where it will help (that is, you've found
a case where the compiler's choice has a significant impact on the
performace of the code.)

If your intent is to keep a pointer to the array in a register, then use

char str[LEN];
register char *const ptr= str;

But it's not likely to help a lot. If the compiler actually uses a
register, the machine code for str[4] and ptr[4] are not likely to
differ by more than a byte, and the number of memory references will
probably be the same.



Thu, 30 Jan 2003 03:00:00 GMT  
 register char str[LEN]

Quote:

> It seems that when I declare a char array as register, the

Why in the world would you want to do that?

Quote:
> compiler (gcc) returns the warning: address of register
> variable `str' requested

Hehe.

Quote:
> What can be wrong? Is it inherently wrong to declare a char
> array as a register variable?

Hm, almost.  Here is what C99 says in 6.3.2.1 (3):

# Except when it is the operand of the sizeof operator or the
# unary & operator, or is a string literal used to initialize an
# array, an expression that has type array of type is converted
# to an expression with type pointer to type that points to the
# initial element of the array object and is not an lvalue. If
# the array object has register storage class, the behavior is
# undefined.

So there is practically nothing you could actually do with such
an array :-)
--
Nils Goesche
Ask not for whom the <CONTROL-G> tolls.



Thu, 30 Jan 2003 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. char **str vs. char *str[]

2. char *str vs. char str[]

3. char *str vs. char s

4. char* and char str[2]

5. int gethostname(char FAR* name, int len)

6. c standard regarding char str[ ], str[50] & *str

7. Basic Question - char *str V/S int *

8. str != (char *) NULL

9. how to address char * str[] ?

10. char *str - Pointer usage

11. q about array of char str

12. _T(str) vs (CString)str?

 

 
Powered by phpBB® Forum Software