Archived

This topic is now archived and is closed to further replies.

wyrd

WORD, DWORD, LONG or short, int, long, etc?

Recommended Posts

wyrd    112
I've come from VB6, and am now currently a C# programmer. The only C/C++ programming I've done is for school projects, so you'll have to excuse my dumb question regarding the languages (C/C++ that is). I was wondering if I should be adopting the practice of using typedefs for my value types (WORD, DWORD, etc), or the C++ keywords (short, long, etc). Looking at the intellisense for Visual Studio .NET, it shows; typedef unsigned short WORD typedef unsigned long DWORD typedef long LONG // note: I'm a bit fuzzy since I haven't used Assembly in ages, but isn't a DWORD = double word, which is 32 bits (4 bytes). So shouldn't that be an unsigned int, not unsigned long? It seems like almost every bit of source code I come across uses these typedefs, so I'm assuming that's what I should be using. If this is the case, why? The only benefit I see is using them for less typing. Unfortunately I lose the special blue highlight that VS.NET provides for keywords, in which case I see using the above typedefs as making my code less readable. [edited by - wyrd on January 17, 2004 4:19:17 PM]

Share this post


Link to post
Share on other sites
YoshiN    122
A long is typically defined as 32 bits, a short as 16 bits and on a 32 bit processor an int is defined as 32 bits (on a 16 bit processor an int would be defined as 16 bits). This isn't always the case however.

And the reason is to shorten typing as well as abstracting the data type. For example, if I want to store the pixel data from a bitmap, would it make sense to store it as chars or as BYTEs. They may be the same, but to someone reading my code it would make more sense to use the latter.

[edited by - YoshiN on January 17, 2004 4:00:09 PM]

Share this post


Link to post
Share on other sites
Xai    1848
you must be very new to C#, because C# doesn''t have typedefs, those typedefs are for C and C++ only - in C/C++ you need typedefs like that occasionaly to get deterministic type sizes on different platforms (those sizes where the same on win 3.1 and win95 machines, and I guess now on win64) ... in C# the built in sizes are of FIXED size already, so that type of typedef would not be usefull.

There are other uses for typedef not related to getting cetain sizes, but that''s a whole different discussion.

P.S. C# (actually the .NET runtime) and Java both specify the sizes of all such contructs because their world view is backwards from the C point of view ... in C, the code compiles different to represent the underlying platform, in .NET and Java the RUNTIME is written different to make the underlying platforms all LOOK more similarly.

Share this post


Link to post
Share on other sites
wyrd    112
YoshiN: Hmm, I see. Thanks.

Xai:
I think you need to re-read my post. My question was regarding C++, not C#. When I said "regarding the language" I was referring to C++. Although, ironically enough, you did answer my question in a round about way;

"in C/C++ you need typedefs like that occasionaly to get deterministic type sizes on different platforms (those sizes where the same on win 3.1 and win95 machines, and I guess now on win64)"

Thanks

Share this post


Link to post
Share on other sites
Xai    1848
ok, sorry op for the misunderstanding ...

but one more thing ... people do not use those particular typedefs near as much anymore ... but those are from the Windows API ... so nearly all windows C and C++ programs use them somewhere? Because the windows API declares it''s function prototypes with them ...

BeOS had a more modern version of similar typedefs, like this:

int8, int16, int32, int64
uint8, uint16, uint32, uint64

for the obvious things.

But remember, the whole point of C/C++ is to use things like "int" when you want the ideal integer size on the platform you are on, so using an int32 would defeat that purpose - use the specific types only when you need to ... a good example of this is for reading to, writing from binary file formats, assuming int is 32 bits could get you into trouble, and assume long is 32 bits WILL get you into trouble on 64 bit platforms ...

Share this post


Link to post
Share on other sites