86

Possible Duplicate:
size of int, long, etc
Does the size of an int depend on the compiler and/or processor?
What decides the sizeof an integer?

I'm using a 64-bit machine.

$ uname -m
x86_64
$ file /usr/bin/file
/usr/bin/file: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, stripped
$ 

When I ran the following program, I got the sizeof(int) as 4-bytes.

#include <stdio.h>

int main(void)
{
    printf("sizeof(int) = %d bytes\n", (int) sizeof(int));

    return 0;
}

If I'm running a 16-, 32- and 64- bit machine, then doesn't it mean that the size of an integer is 16-, 32- and 64- bit respectively?

In my machine, I found the WORD_BIT is 32. Shouldn't it be 64 on a 64-bit machine?

$ getconf WORD_BIT
32
$ 

And, shouldn't the sizeof(int) be 64-bits (8 bytes) in the above case?

Community
  • 1
  • 1
Sangeeth Saravanaraj
  • 16,027
  • 21
  • 69
  • 98
  • 4
    I'm not a C person, but I believe the size of anything except `char` is compiler-specific, and it's also not necessarily the size of a word on the processor. – Ry- Apr 17 '12 at 18:54
  • http://stackoverflow.com/questions/589575/size-of-int-long-etc – Perception Apr 17 '12 at 18:54
  • 1
    @minitech: char is also compiler specific – Daniel Apr 17 '12 at 18:55
  • Since 4 bytes fits in 8 bytes, it would seem sensible not to overturn the horsecart and assume that a regular int can store more than it can - especially for interoperability. –  Apr 17 '12 at 18:56
  • 19
    `sizeof(char)` is always 1 by definition. `char` is the implied unit returned by sizeof. – Matt K Apr 17 '12 at 18:56
  • 2
    The ugly thing about saying "This is a 64-bit machine" is: 64-bit what? 64-bit data bus? address bus? integer registers? – Matt K Apr 17 '12 at 18:57
  • @mkb: 64 bit ALU size. everything else on 64 bit is not actually 64 bit – Daniel Apr 17 '12 at 18:58

4 Answers4

78

Size of a pointer should be 8 byte on any 64-bit C/C++ compiler, but not necessarily size of int.

Eugene
  • 6,194
  • 1
  • 20
  • 31
  • 13
    This is an important observation. Pointers on 32-bit architecture are 32 bits wide which means they can't address more than 4GB of memory. – c0dehunter Jul 28 '14 at 10:40
  • What's the definition of a 64b compiler? If the address space is restricted to 4GB 32b pointers would work fine. – XTF Apr 28 '17 at 08:34
  • 2
    @XTF Right, and **x32** is a real thing that exists: an architecture for 64-bit CPUs but with 32-bit pointers, `int`s, and `long`s: https://wiki.debian.org/X32Port – underscore_d May 23 '17 at 20:51
  • @underscore_d: I wonder why x32 isn't used more often as a model? Many programs never come close to using even a single gig of memory, and using 64-bit pointers in such programs seem like a pure waste. – supercat Oct 16 '18 at 21:49
64

Doesn't have to be; "64-bit machine" can mean many things, but typically means that the CPU has registers that big. The sizeof a type is determined by the compiler, which doesn't have to have anything to do with the actual hardware (though it typically does); in fact, different compilers on the same machine can have different values for these.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
35

Not really. for backward compatibility it is 32 bits.
If you want 64 bits you have long, size_t or int64_t

Daniel
  • 30,896
  • 18
  • 85
  • 139
  • 22
    `long` is 4 bytes on Windows 64. – Jonathan Leffler Apr 17 '12 at 18:58
  • 11
    @Dani What's abnormal about 64-bit Windows? Linux and Windows choose to implement different [data models](https://en.wikipedia.org/wiki/64-bit#64-bit_data_models) for their 64-bit implementations, doesn't make one more normal than the other. – Praetorian Apr 17 '12 at 19:01
  • 2
    @Prætorian: the fact that `long` is still 32-bits. – Jonathan Leffler Apr 17 '12 at 19:02
  • 12
    This is not accurate. Of the two, Linux has the model which is *chosen*. Windows' model is dictated by backward compatibility cruft probably going back to Windows 3, if not more. – Kaz Apr 18 '12 at 05:44
  • 5
    @Kaz: Though not even the larger part of Windows 98/XP programs work under Windows 7 (let's not even think of Windows 8) without compatibility hack, and even then some don't. Insofar, backwards-compatibility to 16-bit Windows is truly a joke. There is no _real_ reason for that. They should have thrown anything older than some threshold overboard with Win 7, if you ask me (because most of it doesn't work properly anyway). Alas, that didn't happen :( – Damon Jul 03 '13 at 12:53
  • @Damon you're not thinking like someone who has business clients. They have a few programs extremely important which would be extremely costly to port, and will support them as long as they are paid enough to do so -to the end of time if needed. They don't care about "the majority" of programs if they don't pay ;) – Francesco Dondi Jan 27 '17 at 08:18
  • 1
    @FrancescoDondi: Oh I think exactly like someone who has business clients. At some point, it's time to tell them: "Look, this 25 year old shit should be replaced by something that works". Maintaining prehistoric software and living with its quirks outpaces a proper solution very quickly in cost (but of course, since as maintainer you're paid for that, doing things better is not in your interest!). Mind you, we are still base-64 encoding attachments in emails as of 2017 on the assumption that there exist modems which do not properly forward 8-bit bytes. I mean... get real. – Damon Jan 27 '17 at 12:02
12

In C++, the size of int isn't specified explicitly. It just tells you that it must be at least the size of short int, which must be at least as large as signed char. The size of char in bits isn't specified explicitly either, although sizeof(char) is defined to be 1. If you want a 64 bit int, C++11 specifies long long to be at least 64 bits.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • 1
    Saying the size of `char` isn't specified explicitly is misleading. `sizeof(char)` is 1 by definition (so a char is a byte). The only "size" which varies is the number of bits it uses: `CHAR_BITS`, which is at least 8. Also note a byte can have more than 8 bits. – Anthales Apr 17 '12 at 19:06
  • 1
    You need to lookup the semantics about the `sizeof` operator. Since I don't have a C++ standard doc I only can tell you what C99 has to say about this (and I know that C++ agrees on this): "The `sizeof` operator yields the size (in bytes) of its operand." and "When applied to an operand that has type `char`, `unsigned char`, or `signed char`, (or a qualified version thereof) the result is 1" – Anthales Apr 17 '12 at 19:16
  • 1
    @Anthales you're absolutely right, §5.3.3 in the C++11 standard. Thanks for pointing that out. – juanchopanza Apr 17 '12 at 19:20
  • 1
    Yeah, it's one of those things, that one either has to know beforehand or that you eventually stumble across, reading the standard. Except no one really wants to read the standard. ;) – Anthales Apr 17 '12 at 19:34
  • Saying that a char is a byte here is fairly meaningless however as a byte is an implementation defined number of bits and not the assumed octet. This is more a backward definition and more defines that a char shall be whatever size the system defines a byte to be. – Vality Feb 24 '14 at 15:22
  • @Vality Yes, but that is the definition. A `char` is size 1, regardless of the number of bits used on any specific platform. – juanchopanza Feb 24 '14 at 15:25