Something I need to know about int vs. long / DWORD

Started by
10 comments, last by ronnybrendel 18 years, 4 months ago
Okay, I'm skimming through Q2 & Q3 source looking for information and I'm also looking for docs on the internet. I see in the source and in many docs that int is used. But, the describe the value as a 4 byte integer (a.k.a. a long). I simply can't judge if I should use a long or not or perhaps a DWORD. But the fact that I see an int be used as a long sort of bothers me. What I mean to say is that it's throwing me off a little. I'm sure there is a good explaination. But I'm just a little worried bout if I should make my own versions for my DIY engine use longs / DWORDs or int.
Take back the internet with the most awsome browser around, FireFox
Advertisement
An int is the integer size native to the processor. For 32-bit x86 compilers this is usually 32 bits, or 4 bytes.

sizeof(long) >= sizeof(int) is guaranteed in the standards. longs on 32-bit x86 compilers are usually the same size as ints.

A DWORD is defined by Windows somewhere in the depths of windows.h. It's 32 bits.

Here's a handy integer type reference.
Ra
Quote:Original post by Ra
An int is the integer size native to the processor. For 32-bit x86 compilers this is usually 32 bits, or 4 bytes.

Actually no. The problem is that about a decade's worth of (badly written) code pretty much assumes that an int is guaranteed to be 32 bits. So both VC and GCC when compiling 64 bit code maintain int at 32 bits. GCC expands long to 64 bits as well as long long, whereas VC uses long as 32 bits and long long as 64 bits.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
typedef unsigned long DWORD;
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
typdef _I_KNEW_THIS_ALREADY_ DWORD_IS_AN_UNSIGNED_LONG ;

Hehe, I know what a DWORD is and I know what the typedef's are. What I was describing is what id and many other developers use an int as a long. The reason why I believe that they do this is because of the variables usage for offsets and sizes. Usually, I would see DWORD (aka unisgned long) as the data type and not int.

The only other option I can think of is that ints are faster then longs some how, and fread( ∫_ptr, 4, 1, fp ) ; works the same as using a long or unsigned long. Or I guess you could cast the value to an int.

I just don't see why they would use an integer for file offsets and data sized when clearly an unsigned long is much more suited for the task.
Take back the internet with the most awsome browser around, FireFox
What are you talking about? In that case, int and long are the SAME thing. What does 'they used an int as a long' mean?

Anyway, sizes should be expressed as std::size_t, but I think the code you're looking might be C.
Quote:Original post by sakky
I just don't see why they would use an integer for file offsets and data sized when clearly an unsigned long is much more suited for the task.

I assume that they were being "lazy", and since using int where unsigned long was proper was unlikely to cause a problem, it was not important enough to fix.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Something thats even more fun its converting old 16bit dos code over to windows XP where they have defined DWORD, WORD etc. already as int etc. based off the 16bit system.

Now of course this would normally be a problem as the variables would just be larger, but the program had to communicate with an embedded system over the serial port which meant that the sizes were important and so the defines needed to be upheld, and so had to convert the defines from int to short and so on. Of course then comes the problem that WORD and stuff are already defined so all the defines in the program need to be renamed too.
Quote:Original post by Dragoncar
Something thats even more fun its converting old 16bit dos code over to windows XP where they have defined DWORD, WORD etc. already as int etc. based off the 16bit system.

Now of course this would normally be a problem as the variables would just be larger, but the program had to communicate with an embedded system over the serial port which meant that the sizes were important and so the defines needed to be upheld, and so had to convert the defines from int to short and so on. Of course then comes the problem that WORD and stuff are already defined so all the defines in the program need to be renamed too.

This is exactly why MS implemented the __intN types...when you need to know exactly how large data is regardless of system, this'll let you. Then, you don't have to change anything when you switch over.

CM
Quote:Original post by Conner McCloud
This is exactly why MS implemented the __intN types...when you need to know exactly how large data is regardless of system, this'll let you. Then, you don't have to change anything when you switch over.

CM


Mmm.... stdint.h

I know it isn't standard C++ but it's available with gcc anyway and if it isn't available on another compiler then how long will it take to write your own version for a particular system?

This topic is closed to new replies.

Advertisement