views:

80

answers:

2

Where is ptrdiff_t defined in C? If non-trivial, how can I make this type visible from GCC on Linux?

+11  A: 

It's defined in stddef.h.


That header defines the integral types size_t, ptrdiff_t, and wchar_t, the functional macro offsetof, and the constant macro NULL.

GMan
Bizarrely, it's located at `linux/stddef.h` (but includes fine with `#include <stddef.h>`. It only contains definition for `NULL` (but including it gives me `ptrdiff_t`). There's some header trickery going on here which prevented me from grepping it in the first place. Can you enlighten?
Matt Joiner
And, of those, only `ptrdiff_t` and `offsetof` are not defined in any other place; the other three are defined by a number of other headers too.
Jonathan Leffler
@Matt: There's no definition of `ptrdiff_t` at all? Strictly speaking, a compiler doesn't have to implement anything in the header. It could get by by simply noting that if `stddef.h` is included, it will internally define `ptrdiff_t` and so on. That could be it, I don't have your version of the header available to look, though.
GMan
@Matt: the headers on Linux are an intricate construction that have to meet a large number of competing standards and requirements. The detailed implementation of the C standard headers is entirely up to the implementation; they need not be files, even (though they most commonly are files). To find something, use (for instance): `grep -R ptrdiff_t /usr/include`.
Jonathan Leffler
The real `stddef.h` is hiding under `/usr/lib/gcc/TARGET/VERSION/include` along with a number of other headers that belong to GCC (and may be GCC-version specific) rather than the C libraries. `linux/stddef.h` is only used for kernel code (and I don't honestly see why they bother having their own copy). You may find the `-H` switch to gcc useful for investigating this kind of question.
Zack
@Zack: Your comment could be an answer by itself. Thanks for clearing this up.
Matt Joiner
+1  A: 

Try <stdint.h> or <cstddef>.

Andy
`cstddef` is C++.
GMan
Actually, I think you mean `stddef.h` - `stdint` holds the min/max macros for a `ptrdiff_t` but not the definition itself.
paxdiablo
Aah. You are right -- stddef was what I wanted. I tend to include both together.
Andy