Is there a connection between the size_t, pointers and amount of ram on a computer?

std::size_t can store the maximum size of a theoretically possible object of any type. Does this have a connection what what values a pointer can store?
Do both of these have a relationship with how much RAM exists in a computer? i.e is any one of them a function of the other?
A pointer has to be able to indicate any addressable piece of memory on the system.

So if a pointer is four bytes long (by which I mean, 32 bits), it can hold the range of numbers from

0000 0000 0000 0000 0000 0000 0000 0000

to

1111 1111 1111 1111 1111 1111 1111 1111


which in decimal is 0 to 4294967295.

So if your pointer is four bytes in size, you literally cannot address more than 4294967296 different memory locations. You'll realise that is exactly 4 GB, and you'll also remember that 32-bit systems had a limit of 4GB of memory (barring extensions and clever dodges and all the rest of it, but for a first order picture of what's going on, this isn't bad).

So the answer to this part of the question is yes, the size of a pointer controls how much memory you can address, and there's no point having more memory than you can actually address.

On a modern 64 bit system, the pointer is 64 bits in size, so it can store a number from 0 to really really high - so high, that you start hitting other limits on how much memory the system can have.

There is no link between the maximum size of an object, and pointers. A pointer is just a single number, and all it has to do is be able to go as high as the amount of memory there is.
Last edited on
Consider this:
1
2
3
4
size_t count;
int * address;
// set some values to both
int * other = address + count;

If the result of the sum is beyond the range of pointers, then other is undefined behaviour.

If size_t is smaller type, then only on very high addresses the sum goes through the roof, but you cannot advance from (very low) address to (very high) address with one increment.

Most programs in practice are far from extremes.
Topic archived. No new replies allowed.