8

There was a question here about equality of sizeof(size_t) and sizeof(void*) and the accepted answer was that they are not guaranteed to be equal.

But at least, must it be that:

sizeof(void*) >= sizeof(size_t)

I think so. Because, take the largest stored object possible in a given C implementation, of size S. Now, the storage area can be thought of as array of bytes of size S. Therefore, there must be a pointer to each byte, and all these pointers are comparable and different. Therefore, the number of distinct elements of type void*, must be at least, the largest number of type size_t, which is unsigned integer type. Thus sizeof(void*) >= sizeof(size_t) .

Is my reasoning making sense or not?

Mark Galeck
  • 6,155
  • 1
  • 28
  • 55

1 Answers1

0

Is my reasoning making sense or not?

The problem with your reeasoning is that you assume that the size of the largest object possible equals SIZE_MAX. But that's not true. If you do

void* p = malloc(SIZE_MAX);

you will (most likely) get a NULL pointer back.

You may also get warnings like:

main.cpp:48:15: warning: argument 1 value '18446744073709551615' exceeds maximum object size 9223372036854775807 [-Walloc-size-larger-than=]
   48 |     void* p = malloc(SIZE_MAX);
      |               ^~~~~~~~~~~~~~~~

Since the maximum object size isn't (always) SIZE_MAX you can't use the value of SIZE_MAX to argue about the size of pointers.

BTW: Some CPU implementation that uses 64 bit pointers at the SW level may not have 64 bit at the HW level. Instead some bits are just treated as all-ones/all-zeros.

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63