446,190 Members | 839 Online
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

# POINTER_MAX and POINTER_MIN??

 P: n/a Is there a way to determine the maximum and minimum values a pointer can hold? Mar 19 '07 #1
10 Replies

 P: n/a coder wrote: Is there a way to determine the maximum and minimum values a pointer can hold? Given that you know what they are when the pointer value is assigned, you just have to keep track of them as you go along (which means you'll be able to know what they are when they're assigned). What actual problem are you trying to solve? Because I've never had a problem I'd describe the way you did, so perhaps my understanding of your problem is flawed. -- Chris "electric hedgehog" Dollin "What I don't understand is this ..." Trevor Chaplin, /The Beiderbeck Affair/ Mar 19 '07 #2

 P: n/a coder wrote: Is there a way to determine the maximum and minimum values a pointer can hold? The question is either ill-formed or meaningful; I'm not sure which. If we're speaking of "J. Random Pointer," the question makes no sense. C does not define a global ordering for pointers, so terms like "maximum" and "minimum" have no meaning. Trying to declare that one pointer is larger than another is like trying to say that sour is larger than sweet. However, C *does* define an ordering for pointers that refer to a single object or a single array of objects or a single allocated memory area. Taking an array, for example, a pointer to the [0] element is the minimum value, and a pointer to one past the final element (the mythical [N] element of an array that goes from [0] to [N-1]) is the maximum. Similarly, in a struct a pointer to the first element is the minimum, and the maximum is a pointer to the "next" identical struct in a (possibly mythical) array. Even in the second case, though, the question is a trifle strange. A pointer into or just past an array has a valid range running from a minimum of array+0 to a maximum of array+N, but that same pointer variable can be made to point to other, completely different places that are not inside the array. It can even be set to NULL and point to nothing at all. So the "valid range" is something of a local and transitory phenomenon, a property of what the pointer is pointing at and not of the pointer itself. -- Eric Sosman es*****@acm-dot-org.invalid Mar 19 '07 #3

 P: n/a On 19 Mar, 12:13, "coder"

 P: n/a On Mar 19, 5:12 pm, Chris Dollin

 P: n/a >>>>"c" == coder

 P: n/a coder wrote On 03/19/07 09:51,: On Mar 19, 5:12 pm, Chris Dollin >coder wrote: >>>Is there a way to determine the maximum and minimum values a pointercan hold? Given that you know what they are when the pointer value is assigned,you just have to keep track of them as you go along (which means you'llbe able to know what they are when they're assigned).What actual problem are you trying to solve? Because I've never had aproblem I'd describe the way you did, so perhaps my understanding ofyour problem is flawed. Well, I'd come across a code in which an integer was assigned the value of a pointer. So, if the value of the pointer can be greater than INT_MAX there could be a problem. That's why I want to ask about the range of values a pointer can hold. (I know that converting pointers to integers is silly, but if there exists an answer to this specific question, I'd like to know it). The conversion isn't "silly," because it is essential in pretty much every malloc() implementation. However, the conversion isn't portable, either, because the result is entirely implementation-defined. The Standard gives a strong hint that the conversion should be as meaningful as possible, but it's only a hint and not a requirement: A system that converted every pointer to the integer -42 and converted every integer to (void*)&stderr would satisfy the Standard. Under the newer C99 Standard, things are a little bit better (although still not completely nailed down). If integer/pointer conversion makes sense on the platform at hand, the = 199101L /* C99 compiler: #endif #ifndef UINTPTR_MAX /* pre-C99, or maybe C99 on weird machine */ #include

 P: n/a coder wrote, On 19/03/07 13:51: On Mar 19, 5:12 pm, Chris Dollin coder wrote: >>Is there a way to determine the maximum and minimum values a pointercan hold? Given that you know what they are when the pointer value is assigned,you just have to keep track of them as you go along (which means you'llbe able to know what they are when they're assigned).What actual problem are you trying to solve? Because I've never had aproblem I'd describe the way you did, so perhaps my understanding ofyour problem is flawed. Well, I'd come across a code in which an integer was assigned the value of a pointer. So, if the value of the pointer can be greater than INT_MAX there could be a problem. That's why I want to ask about the range of values a pointer can hold. (I know that converting pointers to integers is silly, but if there exists an answer to this specific question, I'd like to know it). On some platforms a pointer is larger than an int (or possibly larger than any int type) so it does not make sense. However, sometime for implementation specific reasons it makes perfect sense, for example to access some memory-mapped hardware on a system that allows you to do this (such as accessing the video RAM in DOS or on embedded systems). On most modern desktop systems converting an integer value to a pointer does not make sense because the OS will not let you access memory it did not explicitly give to you, and any time it gives you memory it effectively gives you the pointer (either returned via malloc, or by allocating a variable at an address (you get the address with &, etc). Yes, I know I'm simplifying :-) -- Flash Gordon Mar 19 '07 #8

 P: n/a coder wrote: Well, I'd come across a code in which an integer was assigned the value of a pointer. So, if the value of the pointer can be greater than INT_MAX there could be a problem. That's why I want to ask about the range of values a pointer can hold. C90 doesn't have an integer type that is guaranteed to be hold an (object) pointer value without loss. (And the conversion from pointer to int and back again has /implementation-defined/ results.) So yes, there's a potential problem. Fix (a): don't store pointers in integers, since lots of code doesn't need to do it. Fix (b): since it's implementation-defined, use implementation-specific means to find a big enough integer type [if there is one], and use `typedef thatIntegerType myConvenientName;` to name it so that there's only one place that needs to change. Fix (c): if you have a C99-compliant, or compliant-enough-for-your- purposes, compiler, you can #include

 P: n/a Chris Dollin

 P: n/a On 19 Mar 2007 05:13:41 -0700, "coder" wrote: >Is there a way to determine the maximum and minimum values a pointercan hold? Minumum and maximum imply that one of the relational operators can be applied to the pointer. Consequently: For a pointer pointing to a defined object, the minimum value a pointer can have is the starting address of the object. The maximum value is one past the end of the object. For a pointer pointing to a dynamically allocated area of memory, the minimum value is the address returned by malloc. The maximum value is this minumum value incremented by the number of bytes allocated. When a pointer contains NULL or a value not related to an object or a dynamic area of memory, the relational operators cannot be applied and the concept of greater than is not defined so neither is maximum. If you have C99 and want to treat pointer values as integers, then you could probably make the argument that the most negative value for intptr_t is the minimum value and the most positive value for uintptr_t is the maximum. Remove del for email Mar 20 '07 #11

### This discussion thread is closed

Replies have been disabled for this discussion.