1

I'd like to know at compile-time the range of values for a pointer type. limits.h only specifies maximums and minimums for pure number types. I don't wish to use hard-coded constants, and I prefer not to compute a max using sizeof(foo*).

Dhskjlkakdh
  • 11,341
  • 5
  • 22
  • 17
  • 9
    Explain the problem you are trying to solve rather than ask how to implement the solution you have selected. I smell something very pungent here. – Amardeep AC9MF Jul 15 '10 at 19:16
  • You want the compiler to detect how much memory is available in the system?? – Edward Strange Jul 15 '10 at 19:19
  • 1
    Other than NULL, the actual values in pointers are meant to be opaque. Even NULL isn't guaranteed to have any sort of bit pattern, just that it's unique. If you just want to know 32-bit vs. 64-bit architecture, there are compiler-specific `#defines` that will tell you that. – Eclipse Jul 15 '10 at 19:20
  • 1
    re: Amardeep, a templated trie container class is parameterized on the maximum and minimum values of the type used as the token of its provided key type. I'd like to use a list of pointers as my key type, therefore giving a pointer as the token type. To fulfill the parameterization, I need the range of valid pointer values. – Dhskjlkakdh Jul 15 '10 at 19:25
  • 1
    _Why_ do you not want to compute a maximum using `sizeof`? – James McNellis Jul 15 '10 at 19:40
  • re: James McNellis, it feels like the wrong way to do it. But if you think that's the best solution, then go ahead and write it as an answer to the question. – Dhskjlkakdh Jul 15 '10 at 19:48
  • @sjbach, while that is a very defensible answer, it still seems to me that 'pointer abuse' is taking place without an adequate explanation of why. Explain what problem are you trying to solve with that pointer/tree arrangement and someone might be able to suggest a more conventional way of solving the problem without using pointers in a way they aren't really intended to be used. You are asking for an idiomatic implementation to a very non-idiomatic design approach. – Amardeep AC9MF Jul 15 '10 at 19:54
  • Amardeep, I am mapping a string of objects to values. The expected make up of the string of objects suggests trie lookup as being faster than hash lookup. But it would be too expensive to include a special-purpose token member in this class of object specifically for trie lookup; so, it's convenient for me to use object pointer values, as they are readily and performantly comparable. – Dhskjlkakdh Jul 15 '10 at 20:23
  • @sjbach: I suspect that your trie container class is just going to end up interpreting the pointers as integers anyway, in which case computing based on sizeof(foo*) is fine. How is this trie going to work? What constitutes one 'letter', one byte? – Joseph Garvin Jul 15 '10 at 20:23

2 Answers2

3

I believe I would use intptr_t. It is defined to be the integer that can hold a pointer value, so the min/max values of intptr_t should work.

It might be larger than the values of an actual pointer. But from your explanation of a class that just needs min/max values, I don't believe that you need complete accuracy.

Zan Lynx
  • 53,022
  • 10
  • 79
  • 131
  • 4
    Note that `intptr_t` is not currently part of C++; it was added to C in C99 and will be present in the forthcoming C++0x standard. – James McNellis Jul 15 '10 at 19:42
3

Pointers are not numbers. In particular, they're not absolutely ordered - given two random pointers p and q, you cannot subtract one from another and get a meaningful result - it is U.B., unless they both point to the same object (malloc memory block, static or automatic object, etc). So the concept of a permitted range of pointers is meaningless in Standard C++.

Pavel Minaev
  • 99,783
  • 25
  • 219
  • 289
  • That's informative, but I'm not sure it's germane. The only pointer arithmetic being done is comparison, and according to [this answer](http://stackoverflow.com/questions/1418068/what-are-the-operations-supported-by-raw-pointer-and-function-pointer-in-c-c/1418152#1418152), pointers to separate objects are safely and consistently comparable with `std::less<>`. Presumably if there's an absolute ordering there's also an absolute range. – Dhskjlkakdh Jul 15 '10 at 21:40
  • 1
    Applying operators `<` and `>` in this case would be similarly undefined. But that's a good point regarding `std::less`, actually - though it does not have to be defined in terms of `<` for pointers (the Standard is fairly clear on that) - it still defines an absolute ordering, and, therefore, a range. It doesn't have to map to actual addresses, though, and I'm pretty sure that there's no way to find out the minimum and maximum (in terms of `std::less`) values without enumerating them all. – Pavel Minaev Jul 15 '10 at 23:25