Re: C Interpreter and sizeof operator
- From: "Mike Wahler" <mkwahler@xxxxxxxxxxxx>
- Date: Tue, 29 Nov 2005 23:17:18 GMT
"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
.
- References:
- C Interpreter and sizeof operator
- From: ozbear
- C Interpreter and sizeof operator
- Prev by Date: Re: Why different from expectation
- Next by Date: Re: sort an array of strings
- Previous by thread: C Interpreter and sizeof operator
- Next by thread: Re: C Interpreter and sizeof operator
- Index(es):
Relevant Pages
|