6

I am wondering why in the following program sizeof(int) returns a different value than sizeof(int*).

Here is the small program:

int main(){
    std::cout<<sizeof(int)<<endl;
    std::cout<<sizeof(int*)<<endl;
    return 0;
}

And here is the output:

4
8

Till now I remember the size of a integer pointer is 4byte(gcc compiler). How can I check the correct size of a pointer? Is it computer dependent?

I am running ubuntu 12.04

# lsb_release -a

Distributor ID: Ubuntu 
Description: Ubuntu 12.04 LTS 
Release:    12.04 
Codename:   precise

Is the size of pointer is not constant(standard size) 8 bytes.

quamrana
  • 37,849
  • 12
  • 53
  • 71
yogi
  • 231
  • 2
  • 3
  • 8

4 Answers4

19

The size of an int and an int* are completely compiler and hardware dependent. If you're seeing eight bytes used in an int*, you likely have 64-bit hardware, which translates into eight bytes per pointer.

Hope this helps!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • 6
    "You likely have a 64-bit operating system." Indeed, but more importantly, your process is likely a 64-bit process. :-) – James McNellis Jun 11 '12 at 17:21
  • 3
    I'd like to add, that the only type whose size is defined by standard is `char`. `sizeof(char)` is always `1` – LihO Jun 11 '12 at 17:22
  • 1
    @LihO: The size of a `char` is not actually defined by the standard, but `sizeof` express is results in _number of chars_. – K-ballo Jun 11 '12 at 18:46
  • @K-ballo: I think that was true before C99 since [C99 section 6.5.3.4](http://c0x.coding-guidelines.com/6.5.3.4.html) says: *"The sizeof operator yields the size (in bytes) of its operand"*. – LihO Jun 11 '12 at 18:53
  • @Liho: Unless they have fixed the number of bits in a char _(never gonna happen)_... a `char` is one _byte_ but a _byte_ size is implementation defined. – K-ballo Jun 11 '12 at 18:56
10

sizeof(char) == 1

There are no other guarantees(*).

In practice, pointers will be size 2 on a 16-bit system, 4 on a 32-bit system, and 8 on a 64-bit system.


(*) See the comment of James Kanze.
gliderkite
  • 8,828
  • 6
  • 44
  • 80
  • 2
    `sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)` is also guaranteed. As is `sizeof(float) <= sizeof(double) <= sizeof(long double)`. (There's also a guarantee that all sizes are integral, but that's more or less understood.) – James Kanze Jun 11 '12 at 18:02
  • @JamesKanze Wanting to be pedantic yes, good comment. I tried to write the answer as clear as possible according to the type of question and questioner. – gliderkite Jun 11 '12 at 18:06
  • Pointers are generally 2 bytes on 8-bit systems too, such as Arduino Uno (ATmega 328 microcontroller). – Gabriel Staples Mar 26 '23 at 17:22
5

The size of a pointer is system, compiler, and architecture-dependent. On 32-bit systems it will typically be 32 bits while on 64-bit systems they will typically be 64 bits.

If you're trying to store a pointer into an integer for later restoration into the pointer again you can use the type intptr_t which is an integral type big enough to hold (I believe) normal (non-function) pointer types.

Mark B
  • 95,107
  • 10
  • 109
  • 188
2

For 32-bit systems, the 'de facto' standard is ILP32 - that is, int, long and pointer are all 32-bit quantities.

For 64-bit systems, the primary Unix 'de facto' standard is LP64 - long and pointer are 64-bit (but int is 32-bit). The Windows 64-bit standard is LLP64 - long long and pointer are 64-bit (but long and int are both 32-bit).

At one time, some Unix systems used an ILP64 organization.

None of these de facto standards is legislated by the C standard (ISO/IEC 9899:1999), but all are permitted by it.

and

If you are concerned with portability, or you want the name of the type reflects the size, you can look at the header , where the following macros are available:

int8_t int16_t int32_t int64_t

int8_t is guaranteed to be 8 bits, and int16_t is guaranteed to be 16 bits, etc.

See this question.

Community
  • 1
  • 1
  • There's no guarantee that `int8_t` etc. exist. For maximum portability, just use `int`, unless you know you need something larger, and validate your input. – James Kanze Jun 11 '12 at 18:03
  • @JamesKanze: ??? is fairly standard. http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/inttypes.h.html –  Jun 11 '12 at 20:01
  • @OAOD `` is standard C and C++ header in which these types are defined, and in ``, `int8_t` et al. are marked as optional. In the C standard, I think they are "required" if the hardware supports them; that is at any rate the intent. But they must be exact size types, and the signed types must be 2's complement. On machines which don't have 8 bit bytes, or which aren't 2's complement, they won't be defined. (Posix requires them, which limits the hardware on which it can be implemented.) – James Kanze Jun 12 '12 at 07:40