0

I cannot understand the purpose of size_t and how it works. I read many articles about it, but I could not find a clear explanation, yet. For now, what I know about this data type is:

  • It is for objects in memory.
  • Library functions that take or return sizes expect them to be of type or have the return type of size_t.
  • It is the result of the sizeof operator.
  • It is defined in stddef.h, and is unsigned int type which has at least 16 bits.

Please give me more information about what it does and why we need this. In addition, I saw some people mentioning that the size_t's capacity changes depends on its implementation(ex: 64bit / 32 bit system?). Please give me some information about its relationship to its implementation, too.

Thank you for your support.

Tom_the_cat
  • 159
  • 12
  • 1
    It's just a type with a standard name. It was added to standard C (C89/C90) in large part to give a name to the type returned by the `sizeof` operator, which couldn't be mandated to be `unsigned int` or `unsigned long` because different implementations needed to use different types. – Jonathan Leffler Aug 15 '18 at 19:13
  • 2
    It is simply an unsigned integer type. You could use `unsigned int` or `unsigned long`, except which you should use varies from one C implementation to another. In some C implementations, you might even need something wider than `unsigned long` for sizes. Instead of making you guess or modify the code for each implementation, the C standard says you can use `size_t`, and it will automatically be an unsigned integer type suitable for sizes, because the C implementation is responsible for defining it appropriately. – Eric Postpischil Aug 15 '18 at 19:15

0 Answers0