Re: C Interpreter and sizeof operator



In article <438cdcef.9338968@news-server>, ozbear@xxxxxxxxxxx (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?
>
> 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?
> Or is it perfectly acceptable for the sizeof operator to different
> results?

sizeof (foo) must be equal to sizeof (bar).

memcpy (&foo, &bar, sizeof (foo)) must have exactly the same effect as a
simple assignment foo = bar. And that assignment must work, so your
interpreter must change the memory allocated to foo from 2 byte to 4
byte.

If I write a function

void f (int* p, int value) { *p = value; }

then calling

f (&foo, 2000000000)

must work. Basically, everything must worked as guaranteed by the C
Standard. As long as the interpreter makes sure that everything works as
it should, it is free to do whatever it likes.
.



Relevant Pages

  • Re: Null terminated strings: bad or good?
    ... conforming, which doesn't include any program that does a ... for sizeof is to have an implementation limi which is a maximum size for ... this is not something the standard says explicitly; ... standard's specifications for the semantics of sizeof are otherwise ...
    (comp.std.c)
  • Re: Enumerations in fortran 2003
    ... >>> that the OP is asking if F2003 enumerators are to be the same ... In particular, the sizeof ... That doesn't match my understanding of the C standard. ...
    (comp.lang.fortran)
  • Re: Whats the deal with size_t?
    ... dimensions are unknown at the time of writing. ... If you're talking about address ) + offset, ... [why does the compiler know about sizeof but not about size_t? ... standard, defualt index variable, which doesn't hold a size. ...
    (comp.lang.c)
  • Re: Null terminated strings: bad or good?
    ... describing the sizeof ... This second reading distinguishes between the "ideal" or nominal ... My position is that this second reading is how the standard ... This statement makes it clear that the intended interpretation ...
    (comp.std.c)
  • Re: Null terminated strings: bad or good?
    ... to say how to deal with the apparent inconsistency. ... The result of sizeof being implementation-defined is the loophole ... because no strictly conforming program can do ... The requirements of the standard do not just apply to strictly ...
    (comp.std.c)