473,657 Members | 2,493 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C Interpreter and sizeof operator

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?

Regards, Oz
--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 29 '05 #1
12 1862

"ozbear" <oz****@bigpond .com> wrote in message
news:438cdcef.9 338968@news-server...
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
Nov 29 '05 #2
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.
Nov 30 '05 #3
In article <438cdcef.93389 68@news-server>, oz****@bigpond. com (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.
Nov 30 '05 #4
In article <43************ ***********@new s.xs4all.nl>,
Skarmander <in*****@dontma ilme.com> wrote:
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).


So if I write

long long i;
char array [100];

for (i = 0; i < 100; ++i) array [i] = i;

the compiler is free to use 1, 2, 3, 4 or any other number of bytes for
i, because it doesn't make any difference to the code. The compiler is
allowed to cheat, as long as you cannot detect that it cheats.
Nov 30 '05 #5
Christian Bau wrote:
In article <43************ ***********@new s.xs4all.nl>,
Skarmander <in*****@dontma ilme.com> wrote:
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).


So if I write

long long i;
char array [100];

for (i = 0; i < 100; ++i) array [i] = i;

the compiler is free to use 1, 2, 3, 4 or any other number of bytes for
i, because it doesn't make any difference to the code. The compiler is
allowed to cheat, as long as you cannot detect that it cheats.


Yes. The compiler is even free not to use any bytes for i at all and emit a
fully initialized array in the object file somewhere, if this is the only
piece of code that assigns to 'array'.

Obviously, the burden is on the compiler writer to make sure these
optimizations are always safe, but as long as programs have the required
semantics, the standard couldn't care less.

S.
Nov 30 '05 #6
Christian Bau wrote:

memcpy (&foo, &bar, sizeof (foo)) must have exactly the same effect
as a simple assignment foo = bar.

Not exactly. The assignment may be done on an element by element basis,
which would not copy the padding bytes.

For the valid data itself, the two structs will be same of course, but
a bitwise comparison would not necessarily be the same.

That is:

memcmp(&foo, &bar, sizeof foo)

need not evaluate to 0 following foo = bar.

Brian

Nov 30 '05 #7
Note: Brian snipped the declaration of foo and bar.
int foo = 0;
int bar = 200000000;

Brian (Default User) wrote:
Christian Bau wrote:
memcpy (&foo, &bar, sizeof (foo)) must have exactly the same effect
as a simple assignment foo = bar.


Not exactly. The assignment may be done on an element by element basis,
which would not copy the padding bytes.

For the valid data itself, the two structs will be same of course, but
a bitwise comparison would not necessarily be the same.

That is:

memcmp(&foo, &bar, sizeof foo)

need not evaluate to 0 following foo = bar.


What if, as in this case, foo and bar are both ints? Must memcmp
evaluate to zero following foo = bar?

--
Simon.
Dec 1 '05 #8
Simon Biber wrote:
Note: Brian snipped the declaration of foo and bar.
int foo = 0;
int bar = 200000000;

Brian (Default User) wrote:
Christian Bau wrote:
memcpy (&foo, &bar, sizeof (foo)) must have exactly the same effect
as a simple assignment foo = bar.


Not exactly. The assignment may be done on an element by element basis,
which would not copy the padding bytes.

For the valid data itself, the two structs will be same of course, but
a bitwise comparison would not necessarily be the same.

That is:

memcmp(&foo, &bar, sizeof foo)

need not evaluate to 0 following foo = bar.


What if, as in this case, foo and bar are both ints? Must memcmp
evaluate to zero following foo = bar?


No. ints can have padding bits. Assignment need not make these padding bits
equal, since all that is required is that the "value" of 'bar' is copied to
'foo'. Values may have multiple representations .

S.
Dec 1 '05 #9
Skarmander wrote:

Simon Biber wrote:
Note: Brian snipped the declaration of foo and bar.
int foo = 0;
int bar = 200000000;

Brian (Default User) wrote:
Christian Bau wrote:
memcpy (&foo, &bar, sizeof (foo)) must have exactly the same effect
as a simple assignment foo = bar.

Not exactly. The assignment may be done on an element by element basis,
which would not copy the padding bytes.

For the valid data itself, the two structs will be same of course, but
a bitwise comparison would not necessarily be the same.

That is:

memcmp(&foo, &bar, sizeof foo)

need not evaluate to 0 following foo = bar.


What if, as in this case, foo and bar are both ints? Must memcmp
evaluate to zero following foo = bar?


No. ints can have padding bits. Assignment need not make these padding bits
equal, since all that is required is that the "value" of 'bar' is copied to
'foo'. Values may have multiple representations .


But, on systems that have padding bits, would copying them have any effect?
Does overwriting any padding bytes in structs cause any "bad" behavior?

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer .h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>

Dec 1 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

70
8859
by: Roy Yao | last post by:
Does it mean "(sizeof(int))* (p)" or "sizeof( (int)(*p) )" ? According to my analysis, operator sizeof, (type) and * have the same precedence, and they combine from right to left. Then this expression should equal to "sizeof( (int)(*p) )", but the compiler does NOT think so. Why? Can anyone help me? Thanks. Best regards. Roy
2
2456
by: Xiangliang Meng | last post by:
Hi, all. What will we get from sizeof(a class without data members and virtual functions)? For example: class abnormity { public: string name() { return "abnormity"; }
31
2360
by: Anjali M | last post by:
#include <stdio.h> int main() { int number = 10; printf("Number is: %d\n", number); printf("Sizeof of number is: %d\n", sizeof(number++)); printf("Number now is: %d\n", number);
7
1925
by: dam_fool_2003 | last post by:
#include<stdio.h> int main(void) { unsigned int a=20,b=50, c = sizeof b+a; printf("%d\n",c); return 0; } out put: 24
42
2389
by: Christopher C. Stacy | last post by:
Some people say sizeof(type) and other say sizeof(variable). Why?
12
2407
by: sonu | last post by:
#include<stdio.h> main() { int x=10,y; y=sizeof(++x); printf("x=%d\ny=%d\n",x,y); } Oput Put
15
2233
by: Alex Vinokur | last post by:
Why does one need to use two kinds of sizeof operator: * sizeof unary-expression, * sizeof (type-name) ? Their behavior seem not to be different (see an example below). ------ C++ code ------ #include <iostream> using namespace std;
43
781
by: Richard | last post by:
Could someone point me to why "sizeof x" is/isnt preferable to "sizeof(x)",
28
6734
by: Howard Bryce | last post by:
I have come across code containing things like sizeof int How come that one can invoke sizeof without any parentheses surrounding its argument? Is this admissible within the standard? Can it in general be done with one argument functions? Why would one want to do it anyway, other than showing that one knows and to save two characters?
0
8421
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8325
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8844
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8742
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8518
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7354
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6177
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.