Re: Test if pointer points to allocated memory
From: Jack Klein (jackklein_at_spamcop.net)
Date: 01/22/04
- Next message: Richard Heathfield: "Re: Test if pointer points to allocated memory"
- Previous message: Scott Fluhrer: "Re: clc-compliant pseudo-random number generator"
- In reply to: E. Robert Tisdale: "Re: Test if pointer points to allocated memory"
- Next in thread: Richard Heathfield: "Re: Test if pointer points to allocated memory"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 22 Jan 2004 06:46:43 GMT
On Wed, 21 Jan 2004 21:27:17 -0800, "E. Robert Tisdale"
<E.Robert.Tisdale@jpl.nasa.gov> wrote in comp.lang.c:
> Jack Klein wrote:
>
> > E. Robert Tisdale wrote:
> >
> > Undefined behavior.
> > p and &p are not pointers to the same object
> > or one past the same object or array.
>
> Please elaborate. Why does that make a difference?
Paragraph 5 of ISO 9899:1999 section 6.5.8 "Relational operators", a
little thing that defines the standard C language:
<quote>
When two pointers are compared, the result depends on the relative
locations in the address space of the objects pointed to. If two
pointers to object or incomplete types both point to the same object,
or both point one past the last element of the same array object,
they compare equal. If the objects pointed to are members of the same
aggregate object, pointers to structure members declared later compare
greater than pointers to members declared earlier in the structure,
and pointers to array elements with larger subscript values compare
greater than pointers to elements of the same array with lower
subscript values. All pointers to members of the same union object
compare equal. If the expression P points to an element of an array
object and the expression Q points to the last element of the same
array object, the pointer expression Q+1 compares greater than
P. In all other cases, the behavior is undefined.
</quote>
> >> // p probably points to a character in automatic storage
> >> // (the program stack)
> >
> > Not all processors have a stack.
>
> But they all have "automatic storage".
> The typical implementation of automatic storage is on the program stack.
>
> > Even for those that do, you are
> > you are making the unwarranted and unproven assumption that
> > "the stack" resides at higher memory addresses than other areas.
> > I know of several architectures
> > where processor hardware requires that the stack be in low memory.
>
> Name one.
All 8051 processors, limited to lowest 256 8-bit bytes.
All Philips XA processors, limited to lowest 64K 8-bit bytes.
All Texas Instruments TMS320C28xx DSPs, limited to lowest 64K 16-bit
bytes.
Once you leave the common desktop behind, there are quite a few
hardware architectures that limit their hardware stack to specific
regions of memory for a variety of reasons.
> >> }
> >> else {
> >> // p probably points to static data or free storage
> >> }
> >
> > Or is uninitialized. Or null.
> > Or points to allocated memory that has been free.
> >
> >>But, of course, the ANSI/ISO C standards do *not* specify this.
> >
> > Neither does anyone with any sense, you included.
>
> I don't know whether I have any sense or not.
> But my assertion is easily tested:
All that proves is that the one implementation you know, from which
you probably formed the erroneous impression, works the way it works.
This says nothing at all about any other platform/implementation, or
what the language defines.
>
> > cat main.c
> #include <stdio.h>
> #include <stdlib.h>
>
> int main(int argc, char* argv[]) {
> //char c;
> //char* p = &c;
> char* p = (char*)malloc(sizeof(char));
> if (p > (char*)(&p)) {
> fprintf(stdout, "p probably points to a character "
> "in automatic storage.\n");
> }
> else {
> fprintf(stdout, "p probably points to static data "
> "or free storage.\n");
> }
> return 0;
> }
>
Actually it doesn't even prove that, since you neglected to show the
output of the program.
-- Jack Klein Home: http://JK-Technology.Com FAQs for comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html comp.lang.c++ http://www.parashift.com/c++-faq-lite/ alt.comp.lang.learn.c-c++ http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
- Next message: Richard Heathfield: "Re: Test if pointer points to allocated memory"
- Previous message: Scott Fluhrer: "Re: clc-compliant pseudo-random number generator"
- In reply to: E. Robert Tisdale: "Re: Test if pointer points to allocated memory"
- Next in thread: Richard Heathfield: "Re: Test if pointer points to allocated memory"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|