Why is sizeof (int) different from sizeof (int *)?

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

Here is a small program:

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

And here is the conclusion:

 4 8 

Until now, I remember that the size of the integer pointer is 4 bytes (gcc compiler). How can I check the correct size of the pointer? Does it depend on the computer?

I am running ubuntu 12.04

 # lsb_release -a Distributor ID: Ubuntu Description: Ubuntu 12.04 LTS Release: 12.04 Codename: precise 

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

+7
source share
4 answers

The size of int and int* depends entirely on the compiler and hardware. If you see eight bytes used in int* , you will likely have 64-bit hardware that translates to eight bytes per pointer.

Hope this helps!

+15
source

sizeof(char) == 1

There are no other warranties (*).

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


(*) See Comment by James Kanze.
+7
source

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

If you are trying to save a pointer to an integer for later recovery to a pointer again, you can use the intptr_t type, which is an integral type large enough to hold the (I think) ordinary (non-functional) pointer types.

+4
source

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

For 64-bit systems, the main Unix de facto standard is LP64 - long and a 64-bit pointer (but int is 32-bit). The 64-bit version of Windows is the standard LLP64 - a long long and a 64-bit pointer (but long and int are 32-bit).

At one time, some Unix systems used ILP64.

None of these de facto standards are governed by standard C (ISO / IEC 9899: 1999), but they are all authorized by them.

and

If you are interested in portability or want the name type to reflect size, you can look at the header where the following macros are available:

int8_t int16_t int32_t int64_t

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

See question .

+2
source

All Articles