[QUOTE=TruongAn;2993155]Here is the size of some data types on my 64-bit system at home.
Compile the above code with g++
Target: x86_64-redhat-linux
gcc version 4.1.2 20070925 (Red Hat 4.1.2-33)
Code:
size of char 1
size of int 4
size of short 2
size of long 8
size of float 4
size of double 8
size of long double 16
size of wchar_t 4
size of int* 8
so the question is why on my 64-bit box size of int is still 4, I think it sould be twice the size of int on 32-bit box?
Moreover, on the 32-bit box size of int and size of long is equal?
QUOTE]
Hi
I believe that there is no prescribed size for an integer in the C programming language, and I've always maintained that the size of an INT is processor-dependent. Whether your impression is correct or not doesn't really matter; a far better approach to any problem is to define your own types either as:
typedef int32 int,
or//
#define int32 int
And these definitions, along with others (e.g. you may have a variety of unsigned and signed integer types such as:
int8, int16, int32
uint8, uint16, uint32
and maybe even a bool that only takes 8 bits of storage instead of the usual 32. (Compiler dependent)
If you follow this approach you should put these definitions into a header file and establish a convention that EVERY source file must include this header file at a minimum. That way, your code becomes more portable; during testing, if you discover that int32 is no longer an int on your machine/compiler - i.e. maybe it's suddenly 8 bytes instead of 4, then you only need to rewrite your header file and then recompile the whole system. That should fix the problem.
Never depend upon the compiler's built-in types in "C" if it matters to your solution that the size of a variable must be 1, 2, 4 or 8 bytes.
My $.02
Regards
Peatmoss/Alleria