0

My question:

  • int
  • float
  • char
  • str

These var types each have a standardized allotment of bytes allocated for them. But I never learned about this for pointers:

These "address" storing variables, whose contents seem like just a series of alphanumeric characters.

C++ code:

int *ptr1;
char *ptr2;
void *ptr3;

So what is the standard memory allocation for the pointers listed above? And in general?

user438383
  • 5,716
  • 8
  • 28
  • 43
Anonymous
  • 31
  • 3
  • 2
    Does this answer your question? [What is the size of a pointer?](https://stackoverflow.com/questions/6751749/what-is-the-size-of-a-pointer) – cSharp May 09 '22 at 07:39
  • [Fundamental types - see Data models section](https://en.cppreference.com/w/cpp/language/types) – Richard Critten May 09 '22 at 08:35

1 Answers1

5

A pointer has the size sizeof(ptr1) bytes and that's about all the standard specifies. In theory, different object pointers can have different sizes, and different function pointers may have different sizes as well.

What it boils down to underneath the hood is that the CPU data size that we usually mean when speaking of 32 bit CPU etc (the max chunk it can process in a single instruction) does not necessarily correspond to the address bus width/addressable memory space. The latter determines the size of pointers.

In practice (simplification):

  • 8 and 16 bit microcontroller systems, as well as old 16 bit PC may have diverse pointer sizes, typically between 8 and 32 bits, where 16 bits is most common. They usually use non-standard keyword such as pointer qualitifers near or far to indicate this.

    Some ISA might have a "zero page", meaning 8 bit pointers to the first 256 addresses, where they can execute code/access data faster (usually this will be on von Neumann MCUs). Similarly, they can have extended memory beyond the normal 65536 bytes, which may give slower execution/data access. 24 bit pointers aren't uncommon.

  • 32 bit systems typically have 32 bit pointers.

  • 64 bit systems typically have 64 bit pointers.

In case you need to grab the absolute address for some reason, then uintptr_t is the portable integer type which should be large enough to hold any pointer address on the given system.

Lundin
  • 195,001
  • 40
  • 254
  • 396