So it is not depending on the processor(32 / 64 bit)?
Yes and no. C is/was designed to be a portable language so you could take your programs and run them on another computer easily. Hence it will make the size of an int whatever you tell it... <snip>
With 64 bit processors, registers (on the mainframe, at least) are 8 bytes. However, we have not seen integers evolve to 8 bytes, and I'm guessing they will be 4 bytes for some time to come. On an IBM 64-bit box, for example, with 8 byte registers, if the 4-byte instructions are used (AKA 31-bit instructions), only the right half of an 8-byte register is used. No muss, no fuss.
Todd
This is how it works with the Core 2 Duo (x64) and the G5 (PPC64) as well. 32-bit mode only uses half of the 8-byte register available, but if you are running a 64-bit clean app (compiled for 64-bit), then your int will be 8 bytes, as will your pointers.
A plain int object has the natural size suggested by the architecture of the execution environment
Yeah, same on the mainframe. It's a complier option to exploit full 64-bit or not. However, the terminology for an "integer" is still 4 bytes. If refering to 64-bit integers, we say "8-byte integers", or, "double-word integers". A word is still 4 bytes.
struct myStruct {
UInt32 myVal; // 4 bytes
UInt16 halfVal;
UInt16 otherHalfVal; // two 16s = 32 bits, or 4 bytes
UInt16 someOtherHalfVal; // only 2 bytes
UInt16 fillerVal; // Fills up the left over 16 bits make this struct 4-byte
// aligned, also gives room for expansion later on
}
Using gcc:
int - 32 bit
long - 32 bit in a 32-bit environment, 64 bit in a 64-bit environment
long long - always 64 bit
#include <stdio.h>
int main (int argc, const char * argv[]) {
printf("Variable Sizes...\n");
printf("short: %d bytes\n", sizeof(short));
printf("long: %d bytes\n", sizeof(long));
printf("int: %d bytes\n", sizeof(int));
printf("long long: %d bytes\n", sizeof(long long));
return 0;
}
Variable Sizes...
short: 2 bytes
long: 4 bytes
int: 4 bytes
long long: 8 bytes
Variable Sizes...
short: 2 bytes
long: 8 bytes
int: 4 bytes
long long: 8 bytes
Results on 32-bit:
Code:Variable Sizes... short: 2 bytes long: 4 bytes int: 4 bytes long long: 8 bytes
Results on 64-bit:
Code:Variable Sizes... short: 2 bytes long: 8 bytes int: 4 bytes long long: 8 bytes
There's something I don't quite understand. sizeof() returns a value of type size_t which, if I remember correctly, is defined as unsigned int. So I guess the results above are fine for 32bit architecture, but for 64 bit size_t wouldn't be big enough for all cases.
Out of interest perhaps you could print out the result of sizeof( size_t )?
thanks
b e n
#if defined(__GNUC__) && defined(__SIZE_TYPE__)
typedef __SIZE_TYPE__ __darwin_size_t; /* sizeof() */
#else
typedef unsigned long __darwin_size_t; /* sizeof() */
#endif
Krevnik
Thanks for taking the time to answer.
b e n
int big_array[ 16000000000 ] ;
…
size_t size = sizeof( big_array )
Well, as an example why size_t needs to be 64bit, you could have something like
Code:int big_array[ 16000000000 ] ; size_t size = sizeof( big_array )
if you had enough memory
b e n
Well, as an example why size_t needs to be 64bit, you could have something like
Code:int big_array[ 16000000000 ] ; size_t size = sizeof( big_array )
if you had enough memory
b e n
Even if size_t was only 32 bit it would be more than big enough to hold the values returned by sizeof(long long). Since sizeof(long long) is 8, it only really needs a four bit unsigned value to store the answer (binary 1000).