C: Why isn't size_t a C keyword?

前端 未结 10 1840
鱼传尺愫
鱼传尺愫 2020-12-28 14:18

sizeof is a C keyword. It returns the size in a type named size_t. However, size_t is not a keyword, but is

相关标签:
10条回答
  • 2020-12-28 14:49

    size_t is not a keyword by necessity. Different architectures often have different sizes for integral types. For example a 64 bit machine is likely to have an unsigned long long as size_t if they didn't decide to make int a 64 bit datatype.

    If you make sizeof a builtin type to the compiler, then it will take away the power to do cross compilation.

    Also, sizeof is more like a magic compile time macro (think c++ template) which explains why it is a keyword instead of defined type.

    0 讨论(0)
  • 2020-12-28 14:52

    The simple reason is because it is not a fundamental type. If you look up the C standard you will find that fundamental types include int, char etc but not size_t. Why so? As others have already pointed out, size_t is an implementation specific type (i.e. a type capable of holding the size in number of "C bytes" of any object).

    On the other hand, sizeof is an (unary) operator. All operators are keywords.

    0 讨论(0)
  • 2020-12-28 14:58

    Does not this signify some kind of a problem in the C standard specification?

    Look up the difference between a hosted implementation of C and a freestanding C implementation. The freestanding (C99) implementation is required to provide headers:

    • <float.h>
    • <iso646.h>
    • <limits.h>
    • <stdarg.h>
    • <stdbool.h>
    • <stddef.h>
    • <stdint.h>

    These headers do not define any functions at all. They define parts of the language that are somewhat compiler specific (for example, the offsetof macro in <stddef.h>, and the variable argument list macros and types in <stdarg.h>), but they can be handled without actually being built into the language as full keywords.

    This means that even in your hypothetical kernel, you should expect the C compiler to provide these headers and any underlying support functions - even though you provide everything else.

    0 讨论(0)
  • 2020-12-28 15:02

    Some headers from the C standard are defined for a freestanding environment, i.e. fit for use e.g. in an operating system kernel. They do not define any functions, merely defines and typedefs.

    They are float.h, iso646.h, limits.h, stdarg.h, stdbool.h, stddef.h and stdint.h.

    When working on an operating system, it isn't a bad idea to start with these headers. Having them available makes many things easier in your kernel. Especially stdint.h will become handy (uint32_t et al.).

    0 讨论(0)
  • 2020-12-28 15:03

    There is no reason not to include stddef.h, even if you are working on a kernel - it defines type sizes for your specific compiler that any code will need.

    Note also that almost all C compilers are self-compiled. The actual compiler code for the sizeof operator will therefore use size_t and reference the same stddef.h file as does user code.

    0 讨论(0)
  • 2020-12-28 15:03

    size_t is actually a type - often an unsigned int. Sizeof is an operator that gives the size of a type. The type returned by sizeof is actually implementation-specific, not a C standard. It's just an integer.

    Edit: To be very clear, you do not need the size_t type in order to use sizeof. I think the answer you're looking for is - Yes, it is inconsistent. However, it doesn't matter. You can still practically use sizeof correctly without having a size_t definition from a header file.

    0 讨论(0)
提交回复
热议问题