Is it safe to assume that a pointer is the size of an int in C?

前端 未结 7 911
别那么骄傲
别那么骄傲 2020-12-20 16:52

In designing a new programming language, is it safe to assume that a C int and a pointer are the same size on the machine?

相关标签:
7条回答
  • 2020-12-20 17:12

    No, especially in 64 bit environments:

    LP64 This covers *nix environments but the same is true in windows for LLP64.

    0 讨论(0)
  • 2020-12-20 17:14

    no, but a pointer should be the same size as a intptr_t.

    0 讨论(0)
  • 2020-12-20 17:21

    I believe the Linux Kernel passes pointers as unsigned long's. They are guaranteed to be at least the same size as a pointer :)

    0 讨论(0)
  • 2020-12-20 17:25

    No, not at all. Many compilers do not have them as the same size.

    0 讨论(0)
  • 2020-12-20 17:29

    No. A pointer may be larger or smaller than an integer in size. If you need to pass a pointer as an integer for some reason (like performing integer, rather than pointer, arithmetic), they are guaranteed to fit into an intptr_t.

    They are not guaranteed to fit into a size_t as suggested in another answer, but in practice it is unlikely that they won't, since the largest addressable size is usually equal to the largest addressable address.

    0 讨论(0)
  • 2020-12-20 17:29

    No; on my MacOS X 10.6.5. machine, an int is 32 bits and a pointer is 64 bits by default.

    If you need an integer that's the right size to hold a pointer too, use #include <inttypes.h> (or <stdint.h>) and uintptr_t - assuming you have C99 support, or can simulate it.

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