Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

size_t is pointer size in practice

Tags:

c

Let me first clarify that I am by now very familiar with definitions of size_t and intptr_t, and I don't want any repetitions of what they accomplish.

Instead I would like to know the following. Do you know of any platform, except x86/DOS (with its unbearable memory models) where the cast

void* a = ...;
size_t b = (size_t)a;

actually loses bits or bytes?

Thanks!

like image 488
Jonas Byström Avatar asked Oct 15 '09 12:10

Jonas Byström


People also ask

Can size_t hold a pointer?

size_t type It is the type of the result returned by sizeof operator. The type's size is chosen so that it can store the maximum size of a theoretically possible array of any type. On a 32-bit system size_t will take 32 bits, on a 64-bit one 64 bits. In other words, a variable of size_t type can safely store a pointer.

What exactly is size_t?

The datatype size_t is unsigned integral type. It represents the size of any object in bytes and returned by sizeof operator. It is used for array indexing and counting. It can never be negative. The return type of strcspn, strlen functions is size_t.

Is size_t larger than int?

If we consider the standard, both are integers of size 16 bits. On a typical 64-bit system, the size_t will be 64-bit, but unsigned int will be 32 bit. So we cannot use them interchangeably. One standard recommendation is that the size_t be at most as big as an unsigned long.

How is pointer size determined?

It depends upon different issues like Operating system, CPU architecture etc. Usually it depends upon the word size of underlying processor for example for a 32 bit computer the pointer size can be 4 bytes for a 64 bit computer the pointer size can be 8 bytes. So for a specific architecture pointer size will be fixed.


1 Answers

AFAIK, on AS/400 pointers are 128-bit, but size_t is defined to be 32-bit.

like image 150
elder_george Avatar answered Oct 18 '22 15:10

elder_george