I was recently part of a little debate on the issue of whether
constants and string literals are considered "data objects" in C. I'm
more confused now than before.
I was always under the understanding that only "named" storage
areas(from the standard) were data objects. ...
Does anyone have any opinions on this?
The C standard has an opinion, and as such, it is pretty much
final
3.15 Object
[#1] A region of data storage in the execution environment,
the contents of which can represent values. Except for
bit-fields, objects are composed of contiguous sequences of
one or more bytes, the number, order, and encoding of which
are either explicitly specified or implementation-defined.
When referenced, an object may be interpreted as having a
particular type; see 6.2.2.1.
Of course, this C99 draft text says "objects", not "data objects",
but since the definition of "object" is "a region of data storage",
prefixing it with the word "data" appears to be benign, albeit
redundant.
Given this definition, a string literal that produces an array clearly
produces an object (or "data object", if you like the extra word):
const char *p = "hello";
even though the object itself has no name.
Ordinary constants, on the other hand, are not "region
of data
storage":
void f(void) {
int i = 3;
...
The constant 3 is not a "region of data storage", nor is it composed
of a "contiguous sequence of one or more bytes", especially if one
happens to be compiling on a SPARC:
mov #3, %l1 ! 3 does not appear as a separate value anywhere
(a "mov" instruction is a pseudo-instruction built out of a bitwise
"or" instruction, in this case, "or"ing %g0 with the signed immediate
13-bit constant that occupies the "simm13" field of the 32-bit
instruction word), or a 680x0:
moveq #3, d4 ! again, 3 does not appear as a separate value
(here the constant 3 is once again embedded within a bitfield in
the instruction word -- and in this case, because the bitfield is
only 3 bits wide, one cannot even isolate a byte with value 3).
(I add the last parenthetical remark because if you dump the
32-bit SPARC instruction as four separate bytes, one of them
*does* have the value 13. But if we just change the line to
read:
int i = 500;
-- which uses a single "mov #500, %l1" instruction -- we find there
is no 16-bit word containing 500, so this is something of a red
herring anyway.)
The named variable i, of course, *is* a "region of data storage"
and is therefore an object. If we take its address and decompose
it into a sequence of "unsigned char"s, we will find the machine's
representation of whatever "int" value i holds at the moment.