Re: C Interpreter and sizeof operator




"ozbear" <ozbear@xxxxxxxxxxx> wrote in message
news:438cdcef.9338968@xxxxxxxxxxxxxx
> 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.

>
> 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 */

Note that type 'int' is only required to have a max value of 32767
(but is allowed to be larger).

Also note that the size of a byte (type 'char') must be at least 8
bits (but is allowed to be larger).

>
> Does the standard require that sizeof foo == sizeof bar

Yes. Each type must have a specific size, iow in your
example, the possible ranges of 'foo' and 'bar' must be
the same.

> thereby
> making this allocation scheme broken, unless hidden in some way?
> Or is it perfectly acceptable for the sizeof operator to different
> results?

No, not for objects of the same type. Why would you want to do this
anyway? What if I later in the code wanted to write:
foo = 10000;

-Mike


.



Relevant Pages

  • Re: Find the size of a datatype
    ... Suppose I am given a datatype say X in C. ... without declaring a variable or pointer variable of that type, ... of course without using sizeof operator. ... standard will tell you how big an int is. ...
    (comp.lang.c)
  • C Interpreter and sizeof operator
    ... If one were writing a C interpreter, is there anything in the standard ... Or is it perfectly acceptable for the sizeof operator to different ... Why is top-posting such a bad thing? ...
    (comp.lang.c)
  • Re: Find the size of a datatype
    ... Suppose I am given a datatype say X in C. ... without declaring a variable or pointer variable of that type, ... course without using sizeof operator. ... standard will tell you how big an int is. ...
    (comp.lang.c)
  • Re: The act of sizeof(++i) ?
    ... It is the C++ standard. ... > Frederick Ding wrote: ... The operand is either an expression, ... > sizeof operator does not evaluate the expression. ...
    (microsoft.public.vc.language)