Home » Community » Forums » General Programming » 64-bit sizeof's
  Intel sponsors gamedev.net search:   
[Control Panel] [Register] [Bookmarks] [Who's Online] [Active Topics] [Stats] [FAQ] [Search]

Add Forum to Favorites |  Send Topic To a Friend | View Forum FAQ | Track this topic


 Last Thread Next Thread 
 64-bit sizeof's
Post New Topic  Post Reply 
I have found a few pages describing 64-bit sizes on unix (though I am looking for Windows), and even one page lists different versions of 64-bit storage.

So I would like to ask,
1. Is there a standard sizeof for int,short,char, etc for 64-bit operating systems?
2. If so, does Windows adhere to them ?
3. If not, I have a borrowed a AMD 64-bit duel core something or another (Its my brother's, he went off to do stuff in Iraq) running Vista 64. Would a short on that system be a short on 32-bit? I know an int shouldn't be.
4. If a char is 8bits on either system, and a short is 16bits, but an int is either 32 or 64, what is 32 bits on a 64-bit system and would that 32bit be forced (padded) to 64bit anyways?

Or is this just something I would have to test for myself?

Thanks for your time!

 User Rating: 1073   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

If you are using C or C++, sizes of types don't have to be the same across different systems. afaik, a short must be >= char, an int >= short and a long >= int.

You can always use sizeof(type) to test in your app.

There are on some compilers other types such as int32_t, int64_t, which specify the bit size, but from what I know these are not standard across all implementations.

If you are sending these types across a network or somesuch, you are better off to serialize them somehow (and just make your app assert the type you are using can hold a certain minimum value).

 User Rating: 1049   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

On most unix, but sadly missing from MSVC, is the header <stdint.h>, which gives you access to explicit integer sizes. However, on common 64-bit desktop systems (i.e. Windows, Mac and many Linux), int remains a 32-bit type, short a 16-bit type, and char a 8-bit type.

Tristam MacDonald - swiftcoding

 User Rating: 1744   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Boost has cstdint typedef's useful for writing portable code that requires certain integer widths.

 User Rating: 1112   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

On my system (XP 64, Visual Studio 2005, 64-bit build):
sizeof(char) == 1
sizeof(short) == 2
sizeof(int) == 4
sizeof(long) == 8
sizeof(float) == 4
sizeof(double) == 8


Steve Macpherson
DirectX MVP and Programmer, Firebrand Games

 User Rating: 2012   |  Rate This User  Send Private MessageView ProfileView JournalView GD Showcase Entries Report this Post to a Moderator | Link

Main difference is for sizeof(T *) which should be 8 on all the 64-bit systems...

 User Rating: 1166   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

The only guarantee C++ makes is that 1 char == 1 byte and then the other >= ones, but the the size of a byte is system dependant.

The only time I can think of where this would be an issue for you is that if you wanted to communicate between 2 completely different platforms that had very thin network layers. But I would imagine the most likely systems you are really going to come across or communicate with will have some flavour of mass market OS which will have nice robust network layers to resolve this case for you, in other cases you will be running on whatever system so it should be all dandy as C++ just says be a byte, whatever a byte is is up to the system.

I suppose if you want to do alot of bit shifting it would be also quite irksome.

Char encoding is where you run into all this different size stuff as well, rather annoying sometimes.

 User Rating: 975   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

On my machine (XP_64, VS2005, 64bit build)
sizeof(long) == 4

Iirc, sizeof(long) == 8 when using the LP64 data model (Linux?). Again iirc, Windows uses LLP64.


 User Rating: 1031   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Quote:
Original post by Icon
On my machine (XP_64, VS2005, 64bit build)
sizeof(long) == 4

Iirc, sizeof(long) == 8 when using the LP64 data model (Linux?). Again iirc, Windows uses LLP64.
Ah, good point - I stand corrected. I was going from memory rather than testing it.

Results from an actual test:
Quote:
sizeof(char) = 1
sizeof(short) = 2
sizeof(int) = 4
sizeof(long) = 4
sizeof(float) = 4
sizeof(double) = 8
sizeof(void*) = 8
sizeof(size_t) = 8



Steve Macpherson
DirectX MVP and Programmer, Firebrand Games

 User Rating: 2012   |  Rate This User  Send Private MessageView ProfileView JournalView GD Showcase Entries Report this Post to a Moderator | Link

Quote:
Original post by Guthur
The only guarantee C++ makes is that 1 char == 1 byte and then the other >= ones, but the the size of a byte is system dependant.


This is not totally correct.
C++ is a near super set of C and includes limits.h which is defined in the C standard as having minimum sizes which are as stated here and "5.2.4.2.1 Sizes of integer types <limits.h>" are:
Quote:

The C standard says that:
CHAR_BIT is at least 8
short has a least 16 bits
int has at least 16 bits
long has at least 32 bits
and long long has at least 64 bits.

Note long long is currently a C type yet not a C++ so we can just disregard that type. The C++ standard includes limits calling it climits and therefore abides by these guarantees which is expressed in C2.4 and 18.2.1.2
Quote:

18.2.1.2
static T min() throw();
Minimum finite value.(181)
...
(181)Equivalent to CHAR_MIN, SHRT_MIN, FLT_MIN, DBL_MIN, etc.


Further as Icon stated there is also other methods of guaranteeing the sizes of types based on the model being used, the list of which includes LP32, ILP32, ILP64, LLP64 and LP64. See this page for information about each model.


So to answer the OP's questions
Quote:
So I would like to ask,
1. Is there a standard sizeof for int,short,char, etc for 64-bit operating systems?

It is dependant on the model being used.
Quote:

2. If so, does Windows adhere to them ?

Windows 64 adheres to the LLP64 model,
Quote:

3. If not, I have a borrowed a AMD 64-bit duel core something or another (Its my brother's, he went off to do stuff in Iraq) running Vista 64. Would a short on that system be a short on 32-bit? I know an int shouldn't be.

If the 32bit systems was windows then yes they would be the same(see 64 bit link above which states the models.)
Quote:

4. If a char is 8bits on either system, and a short is 16bits, but an int is either 32 or 64, what is 32 bits on a 64-bit system and would that 32bit be forced (padded) to 64bit anyways?

If you need sizes of types to be correct across different models and os's then you need to use fixed size types which has already been commented on in this thread.

[edit]
In addition there is an article which tackles some of these points and more related to porting to 64 bit systems which is hosted on this site. You can find the article entitled "20 issues of porting C++ code on the 64-bit platform" here.

[Edited by - magic_man on March 4, 2009 6:46:52 AM]

 User Rating: 896   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

I stand corrected :), nice info to.

 User Rating: 975   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Sorry I couldn't reply earlier. Thanks for everyone's help!



 User Rating: 1073   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Here's what I know (applies to x86-32 and x86-64 only):
long long is 64 bits on both GCC and VC8+ regardless.
long is 64 bits on GCC x64 and 32 bits on VC8+ x64.
int is 32 bits on both regardless.
short is 16 bits on both regardless.

 User Rating: 1895   |  Rate This User  Send Private MessageView ProfileView Journal Report this Post to a Moderator | Link

All times are ET (US)

Post Reply
 Last Thread Next Thread 
Forum Rules:
You may not post new threads
You may post replies
You may not edit your posts
You may not use HTML in your posts
Jump To:
Administrative Options: