-3

When I was reading about pointers at cplusplus, I came across this (quoted) sentence and I have a few questions:

Each one is intended to point to a different data type, but, in fact, all of them are pointers and all of them are likely going to occupy the same amount of space in memory (the size in memory of a pointer depends on the platform where the program runs).

I have two questions:

  1. Why are they going to occupy the same amount of space in memory?

  2. Why does the size in memory of a pointer depends on the platform?

  • 2
    https://en.wikipedia.org/wiki/Pointer_(computer_programming) – RoQuOTriX Jul 21 '22 at 10:47
  • My IDE is Code::Blocks 20.03 if it matters. –  Jul 21 '22 at 11:06
  • 1) cplusplus.com is known to have wrong information. Just don't use it. Use https://en.cppreference.com/ instead. 2) That quote is wrong (and you didn't provide the link to the actual post), [pointer to different types can have different sizes](https://stackoverflow.com/q/71870205/995714) and even [different binary representations](https://stackoverflow.com/q/66102053/995714). 3) The IDE is just a text edit and has no relation whatsoever. The compiler decides everything – phuclv Jul 21 '22 at 11:19
  • @phuclv: The quote isn't wrong on size, as it correctly qualifies the statement (emphasis added) "all of them are *likely* going to occupy the same amount of space in memory". They *can* be different sizes, but on most setups they are the same size. – ShadowRanger Jul 21 '22 at 11:40
  • @ShadowRanger - Tell that to people who worked on systems with segmented memory that had near, far, and huge pointers - pointers of different sizes that point at objects which exist in distinct areas of physical memory. Admittedly, the C or C++ compilers for those platforms extended the language (to allow a programmer explicitly select pointers of different sizes), but some also determined size of a pointer based on analysis about which memory segment objects of particular types should exist in. – Peter Jul 21 '22 at 14:00
  • 1
    @Peter: Yes, I'm well aware of those systems (I've worked on them, briefly, early in my career). That said, those segmented memory systems typically didn't have pointers sizes vary by the type they point to (and as you said, relied on extensions to select sizes which diverges from C++ standard enough to not include it in a general topic about pointers). The other situation (e.g. byte pointers on systems with word-aligned pointers) is similarly rare nowadays. For a modern programmer, they are in fact unlikely to be encountered. It's correct, just omitting details for the uncommon case. – ShadowRanger Jul 21 '22 at 14:52

2 Answers2

3

Simplification: Pointers are postcards, data are homes

A pointer doesn't store the data itself, just the address where you can find the data. In much the same way that you can address a postcard to an apartment in the same space that you address it to a mansion, an adequately sized pointer can point to 1 byte or a terabyte with equal facility.

To answer #2, compare a postcard sent domestically to a postcard sent internationally. The latter requires additional information to address the whole world, so you need a postcard with an extra line for the destination country. Similarly, a pointer on a system with a smaller maximum memory address space (4 GB) only needs a 32 bit pointer, but as soon as they need to address a larger memory space, the pointer must expand to be able to assign unique addresses to every byte.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
1

A pointer is an address. The adress of an object is a number representing the index of the first byte in the virtual memory where the object storage starts.

So regardless of object type, an object pointer on a given architecture will always occupy the same space.

The size of a pointer depends on the architecture. The simplest example is x86 vs x86_64. For the first 32 bits are needed to store addresses (virtual space of a program caps at 2^32 bytes) and for the 2nd one the address space goes up to 2^64 bytes so 64 bits of memory are needed to store a byte addresses aka a pointer.

bolov
  • 72,283
  • 15
  • 145
  • 224
  • FYI, there is an Intel architecture that uses 24-bit addressing. Earlier processors could use 16 bits for pointers. Also, pointers to data may be a different size than pointers to code or functions. – Thomas Matthews Jul 21 '22 at 16:39