Advertisement

sizeof()

Started by November 17, 2000 03:00 PM
4 comments, last by Promiscuous Robot 24 years, 2 months ago
This should be a simple question. Is sizeof() OS dependent or CPU dependent (or even compiler dependent)? I know that on Windows platforms with MSVC++ ints and longs are effectively the same thing. But I have read many times that variable sizes aren''t always the same. Is it based on CPU, OS or compiler? -------------------------- I guess this is where most people put a famous quote... "Everything is funnier with monkey''''s" - Unknown
--------------------------I guess this is where most people put a famous quote..."Everything is funnier with monkey''s" - Unknown
If I remember correctly, the size of a data type is based on the CPU and OS.



- Goblineye Entertainment
The road to success is always under construction
Goblineye EntertainmentThe road to success is always under construction
Advertisement
Yes, that''s right!
The size of the data is both CPU and OS dependent, but they follow a standard, in a 16 bit world, like DOS/Intel, they are:
char - 8 bit
int - 16 bit
short int - 16 bit
long - 32 bit

In a 32 bit world, like Win32/Intel, they are:
char - 8 bit
int - 32 bit
short int - 16 bit
long - 32 bit

As you can see, only the CPU''s native word length changes, in this case, to keep compatibility, you should use "short int" for porting.
Size of pointers usually changes too.

Best regards,

Wenderson Teixeira

The size of a variable depends only on the compiler, not the OS or CPU. It just happens that the compiler writers are smart enough to make data types that match the CPU and OS (A 32-bit CPU works best with 32 bit data types). If I wanted to I could write a C++ compiler for Windows with Pentium processors that only used 8 bit data types. (Don''t know who''d use it, but...)

Micah
There is no standard size; it is compiler dependant, although it is usually fairly consistent across compilers for a particular platform (CPU/OS) based on the native CPU sizes.

The language standard provides only this rule:

sizeof(short) <= sizeof(int) <= sizeof(long)

That''s your only guarantee. I can''t remember if it specifies that sizeof(char)==1 (I think it does), but otherwise you need to test per compiler/platform/os.

---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!
---- --- -- -
New York. New York. New York. Texas. Texas. New York. New York. Canada.
---- --- -- -
ANSI Sec 6.5.3.4:
"When [sizeof is] applied to an operand that has type char, unsigned char,orsigned char, (or a qualified version thereof) the result is 1."

Regarding the sizes of the integer types, the standard provides for the minimum values.
short must be at least two bytes wide, long four, and long long eight. int must also be at least two bytes.

This topic is closed to new replies.

Advertisement