Assigning values to char arrays

B

Ben Bacarisse

James Kuyper said:
Ben Bacarisse wrote:
...

6.5p4 says "Some operators (the unary operator ~, and the binary
operators <<, >>, &, ^, and |, collectively described as bitwise
operators) are required to have operands that have integer type. These
operators yield values that depend on the internal representations of
integers, and have implementation-defined and undefined aspects for
signed types".

As I understand it, that statement about the "internal
representations" refers to the object representation, and does not
imply that the standard attaches any meaning to the representation of
a value that is not currently stored in any C object.

I can accept that this is the intent, but I do not think it is clear
and unambiguous. Since it can't be detected by a C program, I don't
really care if there is one representation (when a value is stored) or
another, transient one, as well. The latter just seemed to me a
convenient expository device for exactly these cases, but if it is a
fiction of my imagination, so be it!
It merely
requires that the bitwise operators act on values in such a way that
if the resulting value were saved in an object of that value's type,
it would have the correct bit pattern.

...

That's exactly how I understand it. I never denied that the allowed
representations were referenced,

Well that was exactly what I thought you were doing when you said:

"In general, the bitwise operations are defined in terms of their
actions on the values, not the representations."

I just can't square this with your last remark above. For four of the
six, the representation is a required part of the definition.
 
C

Chris Torek

(comp.std.c would probably be a better group for this...)

The section called "Representation of types" includes information
which I think is required in order to put a meaning to expressions
like '-1 | -2'. ...
If section 6.2.6 was called "Object representation of types" so that
it was unambiguously about how values are stored and nothing else,
then I believe '-1 | -2' would be undefined. If values, not stored in
objects, do not have a representation, then what "bits" are there to
or together? ...

Indeed. I suspect that the reason the wording in the Standard is
so fuzzy is that various committee members are aware, either
explicitly or via that "something fishy feeling" in the back of
the mind, that there are some situations on some machines where
the representations of values as stored in objects (i.e., in RAM)
differ in some way from the representations when not "stored in
objects" (i.e., loaded into registers).

The most classic and accessible example today occurs on the x86
architecture, when dealing with floating-point values. These are
stored as 4, 8, or 12-byte representations in RAM, but when loaded
into the FPU stack for doing arithmetic, are kept internally as
80-bit (10-byte) representations. Clearly, the "in-register"
version is never quite the same as any "in-memory" version (though
the 10/12 conversions are entirely straightforward). Fortunately,
C forbids using the bitwise operators on floating point values, so
C programmers never[%] see this.

[% What, never? Well, hardly ever. I have an example in
<http://web.torek.net/torek/c/numbers.html> where the difference
shows up, depending on compiler optimizations and/or flags like
gcc's -ffloat-store.]

Ultimately, the fact is that "values" have representations (possibly
more than one for any given value), and "values stored into memory"
(whose address is then taken with the unary "&" operator) have
"object representations" in RAM, but these need not be quite the
same. The bitwise operators (binary &, |, and ^) work with "value
representations". On modern CPUs, the value representations and
object representations usually match (with the exception of x86
floating point, anyway), so that one need not be concerned about
this. It takes unusual circumstances -- such as using a V9 SPARC's
MMU to invert the endianness on some but not all pages in a given
process -- to make all the distinctions apparent.
 
B

Ben Bacarisse

Chris Torek said:
(comp.std.c would probably be a better group for this...)

The section called "Representation of types" includes information
which I think is required in order to put a meaning to expressions
like '-1 | -2'. ...
If section 6.2.6 was called "Object representation of types" so that
it was unambiguously about how values are stored and nothing else,
then I believe '-1 | -2' would be undefined. If values, not stored in
objects, do not have a representation, then what "bits" are there to
or together? ...

Indeed. I suspect that the reason the wording in the Standard is
so fuzzy is that various committee members are aware, either
explicitly or via that "something fishy feeling" in the back of
the mind, that there are some situations on some machines where
the representations of values as stored in objects (i.e., in RAM)
differ in some way from the representations when not "stored in
objects" (i.e., loaded into registers).

The most classic and accessible example today occurs on the x86
architecture, when dealing with floating-point values. These are
stored as 4, 8, or 12-byte representations in RAM, but when loaded
into the FPU stack for doing arithmetic, are kept internally as
80-bit (10-byte) representations. Clearly, the "in-register"
version is never quite the same as any "in-memory" version (though
the 10/12 conversions are entirely straightforward). Fortunately,
C forbids using the bitwise operators on floating point values, so
C programmers never[%] see this.

Interesting example. Thanks.

Ultimately, the fact is that "values" have representations (possibly
more than one for any given value), and "values stored into memory"
(whose address is then taken with the unary "&" operator) have
"object representations" in RAM, but these need not be quite the
same. The bitwise operators (binary &, |, and ^) work with "value
representations".

Well I am glad I am not alone in thinking the wording is deliberate
and suggests the existence of a representation that is not simply the
object representation.
 

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

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top