Re: C Interpreter and sizeof operator
- From: Christian Bau <christian.bau@xxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 30 Nov 2005 00:25:43 +0000
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.
.
- References:
- C Interpreter and sizeof operator
- From: ozbear
- C Interpreter and sizeof operator
- Prev by Date: Re: sizeof a union
- Next by Date: Re: C Interpreter and sizeof operator
- Previous by thread: Re: C Interpreter and sizeof operator
- Next by thread: Help wanted on some source codes
- Index(es):
Relevant Pages
|