Why do pointers have a fixed size independent of the pointed type?

Asked

Viewed 1,323 times

8

The space occupied by normal variables (int, float, double, char) obeys the type rule: int occupies 4 bytes, float occupies 4 bytes, double occupies 8 bytes etc.

Why pointers of different types have exactly the same size in bytes (4 bytes depending on your computer architecture) regardless of the type of pointer?

  • 32-bit machine has 32-bit pointers. 64-bit machine has 64-bit pointers. 16-bit machines (or PIC controllers) have 16-bit pointers. Independent of the pointed type

  • There are contexts in which int is 16 bits, mainly for controllers and for older machines. As rare as these contexts are, they exist

3 answers

8

Because a pointer contains a memory address, and all memory addresses are the same size.

For example, imagine that the computer’s memory looks like a street with several houses side by side, numbered as 0, 1, 2, 3... (and yes, they start at zero). Each of these houses stores a byte in memory. Also, you can refer to each of these houses by their address. To represent the address of one of these houses, just the number of the house, after all, there is only one street in this case.

If you are on a 32-bit (32-bit = 4-byte) architecture, then the last street house would be the 4,294,967,295 number, which corresponds to 232 - 1. This is the same as 4 Gb. Therefore, to represent any memory address (memory up to 4 Gb), you will need addresses with 4 bytes.

Finally, note that no matter what type of data you store in these houses, you still need the same number of bytes to represent an address. A pointer to float nothing is more than somewhere in the memory of where you want to read or write a float, and a pointer to int is a place where you want to read or write a int. Regardless of what you want to read or write, the referenced houses are the same and are on the same street, and therefore have addresses of the same type.

4 Gb of memory at most is insufficient for modern computers. Therefore, on 64-bit architectures, addresses have 8 bytes. That’s theoretically enough for 8 exbibytes. But obviously, in practice, there are no computers with that much memory and we are very far from having them. However, if you need anything with more than 4 Gb of memory, the 64-bit architecture is virtually mandatory.

  • Thank you very much for your explanation. I really enjoyed.

5

Because a pointer keeps in itself a memory address, and these all have the same size regardless of the type of data with which to interpret the object at that address.

On a 32-bit platform, memory addresses are 32-bit, so pointers are all this big. On a 64-bit platform, memory addresses will eventually be 64-bit, so pointers already have this width to fit the addresses needed for when computers have more than 1 Tib of main memory...

4

Pointers always have the same size independent of the data type because they store the address of the position where the variable starts. And this address has fixed size according to the architecture in question.

  • Valew man. Thank you so much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.