sizeof operator machine dependent?

Hi all,
I am a beginner in C++ and I have a doubt regarding the memory system.

1
2
3
4
5
6
7
#include <iostream>

using namespace std;
int main(){
    cout << sizeof(int) << endl;
}


The above code gives the output 4 in codeblocks in my system. But when I use turboC++ Compiler(yeah, I know it is very old), then this gives the output of 2.
Why this happens? As far as I know, the memory to be allocated is machine dependent, not compiler dependent.

Thanks in advance,
-Himansh
Last edited on
It is implementation defined, as the standard would say, meaning it depends on the C++ implementation (the compiler).
sizeof operator machine dependent?

The sizeof operator isn't, in itself, machine dependent (or rather, as Peter87 says, implementation dependent.) But the size of an int, is.

And Turbo C++ is a 16-bit compiler, so it works with 16-bit ints. So when you ask it how big an int is, it says 2 bytes (i.e. 16 bits).

Turbo C++ is, as you say, an old, old compiler (orig. release was 1990); it was aimed at (16-bit) DOS and the old Windows (e.g. Windows 3.1) 16-bit O/S.

When you run an app you compiled with Turbo C++ on a modern Windows system, you're not actually running it directly on the system. Instead it's been run by WOW (Windows on Windows), the 16-bit compatibility layer.

Code::Blocks, on the other hand, usually works (on Windows) with the MinGW version of GCC, which can be either the 32-bit or 64-bit version of the compiler. Both use 32-bit ints. (A 64-bit compiler could work with 64-bit ints, but this isn't usually done.)

16-bit case:
- int is 16-bit (same as a short), long is 32-bit, near pointers are 16-bit, and far pointers are 32-bit (the FAR macro means something when you're compiling for 16-bit systems.)

32-bit case:
- int, long and pointers are all 32-bit.

64-bit case
- for 64-bit Linux : long and pointers are 64-bit but int is 32-bit.
- for 64-bit Windows : long long and pointers are 64-bit but long and int are both 32-bit.
- it is, of course, possible for int, long, and pointers to all be 64-bit. For more info, see:
64-bit computing
http://en.wikipedia.org/wiki/64-bit_computing )

The MinGW version of GCC follows the Windows convention, not the Linux one, so the binaries it creates are compatble with Windows (e.g. calling the Win32 API.)

Andy

Windows on Windows
http://en.wikipedia.org/wiki/Windows_on_Windows
Last edited on
Thank you Peter and Andy (special thanks to you for telling about this WOW system).
You cleared my doubt :)

Although, this led to the generation of another doubt.
Does it mean that if we install a 64 bit compiler on a 32 bit system(if possible) then the size of bit will increase? and then the program compiled with this compiler will run on 64-bit WOW?
Or is it (WOW system ) just for lower compatibilities( running 16 bit on 32 bit )?

-Himansh
Does it mean that if we install a 64 bit compiler on a 32 bit system(if possible) then the size of bit will increase?
That isn't supported.

Or is it (WOW system ) just for lower compatibilities( running 16 bit on 32 bit )?
That's correct. It's to support backward compatibility only.
A 32-bit compiler may support a 64-bit long long.
Does it mean that if we install a 64 bit compiler on a 32 bit system(if possible) then the size of bit will increase?

I've added a bit about this to my previous post.

When compiling for Windows, the short answer is no; sizeof(int) is the same.

Or is it (WOW system ) just for lower compatibilities( running 16 bit on 32 bit )?

64-bit code will not run directly on 32-bit Windows. But you could use a virtual machine (virtual PC) to run the 64-bit code.

WOW64 (Windows 32-bit on Windows 64-bit) allows 32-bit code to run on 64-bit Windows.

Note that the 64-bit version of Windows does not support the original, 16-bit WOW so you need either a virtual machine (using a 32-bit version of Windows or DOS) or an emulator (e.g. DOSBox) to run a 16-bit app.

On types: if you need types that stay the same whatever happesn, you can use the typdefs from stdint.h / cstdint.

<cstdint> (stdint.h)
http://www.cplusplus.com/reference/cstdint/

Andy

WoW64
http://en.wikipedia.org/wiki/WoW64
Last edited on
Thanks you all who came forward to help me.
I think that It is clear to me now :)

-Himansh
Topic archived. No new replies allowed.