By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 839 Online
Bytes IT Community
+ Ask a Question
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
Share this Question
Share on Google+
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" <plcoderREMOVET...@gmail.comwrote:
Is there a way to determine the maximum and minimum values a pointer
can hold?
I'm not sure what you are trying to discover or achieve.

If you are thinking of pointers as a being analogous to integers, you
are assuming a flat address space. This is not a helpful point of
view.

While, in certain contexts (i.e. addresses within a common "object"),
an ordering of pointers is supported, the idea of a general maximum
and minimum value is at best implementation-specific.

Mar 19 '07 #4

P: n/a
On Mar 19, 5:12 pm, Chris Dollin <chris.dol...@hp.comwrote:
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.
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).

Thanks.

Mar 19 '07 #5

P: n/a
>>>>"c" == coder <pl***************@gmail.comwrites:

c(I know that converting pointers to integers is silly, but if
cthere exists an answer to this specific question, I'd like to
cknow it).

Converting pointers to and from integers may or may not be silly, but
it is certainly implementation-defined. The C standard is silent on
what the integer representation of a pointer is. You'll need to
consult the documentation for your compiler, C library, or operating system.

Charlton

--
Charlton Wilbur
cw*****@chromatico.net
Mar 19 '07 #6

P: n/a
coder wrote On 03/19/07 09:51,:
On Mar 19, 5:12 pm, Chris Dollin <chris.dol...@hp.comwrote:
>>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.


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 <stdint.hheader defines intptr_t and uintptr_t
as integer types that can be converted to and from void*
pointers without changing the pointers' value. If the types
are present, so are the macros INTPTR_MIN, INTPTR_MAX, and
UINTPTR_MAX, so you can test for their existence with #ifdef.

If you can't be sure you have a C99 compiler (the adoption
of C99 has been slower than that of the original ANSI Standard),
the "practical" approach is to guess that pointers can probably
fit in the same size integer as can hold object sizes. So,
you might want to add something like this to a header file for
use by programs that need to do such conversions:

#if __STDC_VERSION__ >= 199101L
/* C99 compiler: <stdint.hexists */
#include <stdint.h>
#endif
#ifndef UINTPTR_MAX
/* pre-C99, or maybe C99 on weird machine */
#include <stddef.h /* for size_t */
typedef size_t uintptr_t; /* the understudy */
#define UINTPTR_MAX ((uintptr_t)-1)
#endif

.... and then use uintptr_t (I still don't see the, er, point
of intptr_t) in the conversions. On C99 systems that do not
provide uintptr_t this probably won't work, but the reason
they don't provide it is probably because the architecture
is "exotic" and the conversion wouldn't make sense anyhow.

--
Er*********@sun.com
Mar 19 '07 #7

P: n/a
coder wrote, On 19/03/07 13:51:
On Mar 19, 5:12 pm, Chris Dollin <chris.dol...@hp.comwrote:
>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.

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 <inttypes.h[I think] and
use `[u]intptr_t` [ditto].
(I know that converting pointers to integers is silly,
Not always. But not to be undertaken lightly or in ignorance.

--
Chris "over /there/!" Dollin
"- no longer a stranger to the truth." - The Reasoning, /Awakening/

Mar 19 '07 #9

P: n/a
Chris Dollin <ch**********@hp.comwrites:
Fix (c): if you have a C99-compliant, or compliant-enough-for-your-
purposes, compiler, you can #include <inttypes.h[I think] and
use `[u]intptr_t` [ditto].
inttypes.h will work, but stdint.h has [u]intptr_t also and it
has the advantage of being available on freestanding
implementations.
--
"Given that computing power increases exponentially with time,
algorithms with exponential or better O-notations
are actually linear with a large constant."
--Mike Lee
Mar 19 '07 #10

P: n/a
On 19 Mar 2007 05:13:41 -0700, "coder" <pl***************@gmail.com>
wrote:
>Is there a way to determine the maximum and minimum values a pointer
can 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.