Re: C Interpreter and sizeof operator



ozbear wrote:
If one were writing a C interpreter, is there anything in the standard
standard that requires the sizeof operator to yield the same value for
two different variables of the same type?

Yes. (I know this because I looked up the exact same thing lately.) If x and y have the same type, sizeof x = sizeof y must hold without exception.

6.5.3.4: "The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant."

Note that the *value* of the operand is irrelevant. Only its *type* matters. Therefore the size of an object may *not* depend on its value, but only on its type. If x and y are of type 'int', then sizeof x = sizeof(int) and sizeof y = sizeof(int), therefore sizeof x = sizeof y. Bad Things happen if you violate this.

Let's assume that the interpreter does conform to the range values
for, say, type int, but allocates storage for the variables based
on their value.  So, for two variables foo and bar

  int foo = 0;          /* interpreter allocates two bytes */
  int bar = 200000000;  /* interpreter allocates four bytes */

Does the standard require that sizeof foo  == sizeof bar  thereby
making this allocation scheme broken, unless hidden in some way?

Yes.

You can try and fudge this in cases where the application cannot possibly trip over the wrong size, but it's tricky to do this without violating the standard, and in an interpreter it's very unlikely to be of any value. Just pick a constant size for integers (4 bytes happens to be a very common one).

Or is it perfectly acceptable for the sizeof operator to different
results?

No, it's not.

S.
.



Relevant Pages

  • Re: Null terminated strings: bad or good?
    ... The sizeof operator yields the size of its operand, ... If the type of the operand is a variable length array type, ... As far as I can tell, the standard doesn't explicitly forbid naming types larger than SIZE_MAX bytes, or even applying sizeof to such types -- it just describes the semantics of sizeof in a way that is logically impossible for such types. ... What this means, in effect, is that it's actually the size of its operand that is implementation-defined; the fact that the value is implementation-defined does not give the implementation the option of having sizeof yield any value other than "the size of it's operand". ...
    (comp.std.c)
  • Re: Regarding sizeof Operator
    ... "the operand of a sizeof operator is usually not evaluated" ... Although sizeof must evaluate the size of a variable length array under c99 ... "If the type of the operand is a variable length array type, ...
    (comp.lang.c)
  • Re: Null terminated strings: bad or good?
    ... The sizeof operator yields the size of its operand, ... As far as I can tell, the standard doesn't explicitly forbid naming types ... and yield any size_t value whatsoever. ...
    (comp.std.c)
  • Re: Sizeof question
    ... |> | sizeof returns the number of chars that array occupies. ... | On the most common system, a char is 1 byte though. ... operand, which may be an expression or the parenthesized ... operand is a variable length array type, ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Null terminated strings: bad or good?
    ... I think I just found a paragraph in the standard that should solve this ... This should apply when the operand of sizeof is such that the result ... sizeof on an overly large value were undefined behavior, ...
    (comp.std.c)