data type of size 64 bits on a 32 bit processor

K

Kenneth Brody

Ben said:
Kenneth Brody said:
The only thing "register" guarantees is that you can't take
the address of the object. The following definition "works" just
fine on my compiler, even with warnings turned to max:

register char foo[32766];

There are serious pitfalls in trying to use an array with
register class. See 6.3.2.1p3 (in C99). The last sentence is
the important part:

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.

Well, I would never use such a thing in real life. I was just
trying to come up with a quick register example which would
obviously not fit in a register in most current systems. (Perhaps
the DS-9000 has such huge registers?)

In any case, I was momentarily suprised to see the compiler
complain about my trying to use the array it allowed me to
create, and was wondering what, if anything, could be done with
such a construct.

What about register struct? Is that UB as well? (Again, this is
just curiosity.)

Then there's the question "why not simply forbid register arrays
in the first place if their use is UB (or put the UB notice in
the register storage class description)?"

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
K

Keith Thompson

Kenneth Brody said:
What about register struct? Is that UB as well? (Again, this is
just curiosity.)

I don't see any problem with register struct. Member access isn't
defined in terms of pointers. And if a structure is sufficiently
small (say, a 32-bit structure containing bit fields), then putting it
in a register could make as much sense as putting anything else in a
register. (Not that "register" necessarily implies putting it in a
register.)
Then there's the question "why not simply forbid register arrays
in the first place if their use is UB (or put the UB notice in
the register storage class description)?"

Good question. There might have been some concern about breaking
existing code and/or implementations, but I still would think it would
have made sense to forbid register arrays.
 
I

Ian Collins

Keith said:
Good question. There might have been some concern about breaking
existing code and/or implementations, but I still would think it would
have made sense to forbid register arrays.
The risk can always be mitigated through compiler options.
 
K

Kenneth Brody

[Sub-thread started because someone said that "register" on something
too large to fit in a register should be flagged as an error.]

Keith said:
[... "register arrays" invoke UB ...]
I don't see any problem with register struct. Member access isn't
defined in terms of pointers. And if a structure is sufficiently
small (say, a 32-bit structure containing bit fields), then putting it
in a register could make as much sense as putting anything else in a
register. (Not that "register" necessarily implies putting it in a
register.)

Hmm...

================
#include <stdio.h>

struct bigstruct
{
char array[32766];
};

int main(void)
{
register struct bigstruct foo = { "foobar" } ;

printf("%ld\n",(long)sizeof(foo));
printf("%p\n",foo.array);
printf("%s\n",foo.array);
}
================

This "runs just fine" here, giving:

32767
00127F80
foobar

Of course, "runs just fine" is allowed for UB as well.

The strange thing here is that foo.array is addressable. I suppose
this is okay, since register arrays invoke UB. Do arrays within
register structs invoke UB?

(Note that "&foo.array" is accepted by the compiler, but "&foo"
is forbidden by the compiler as "'&' on register variable", as
is expected.)

Or is this just getting a bit too esoteric?

The writers of the Standard obviously felt it necessary to state
explicitly that register arrays invoke UB, but they appear to have
missed register structs.

[...]

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
K

Keith Thompson

Kenneth Brody said:
[Sub-thread started because someone said that "register" on something
too large to fit in a register should be flagged as an error.]

Keith said:
[... "register arrays" invoke UB ...]
I don't see any problem with register struct. Member access isn't
defined in terms of pointers. And if a structure is sufficiently
small (say, a 32-bit structure containing bit fields), then putting it
in a register could make as much sense as putting anything else in a
register. (Not that "register" necessarily implies putting it in a
register.)

Hmm...

================
#include <stdio.h>

struct bigstruct
{
char array[32766];
};

int main(void)
{
register struct bigstruct foo = { "foobar" } ;

printf("%ld\n",(long)sizeof(foo));
printf("%p\n",foo.array);
printf("%s\n",foo.array);
}
================

This "runs just fine" here, giving:

32767
00127F80
foobar

It "runs fine" for me too, but I get a warning

c.c:13: warning: address of register variable `foo' requested

on the line
printf("%p\n",foo.array);

[...]
The writers of the Standard obviously felt it necessary to state
explicitly that register arrays invoke UB, but they appear to have
missed register structs.

Register structs that contain arrays are certainly problematic.
Register structs that *don't* contain arrays are, as far as I can
tell, just fine.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top