Re: Test if pointer points to allocated memory

From: Jack Klein (jackklein_at_spamcop.net)
Date: 01/22/04


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


Relevant Pages

  • parameter passing semantics, member function local variable initialization and NaN
    ... several days ago, I asked a question about NaN, a lot of folks suggested I ... I found one overflowed array index and today I found out that passing ... some objects as parameter instead of passing pointers to those object will ... cause the stack get overflowed. ...
    (comp.unix.programmer)
  • Re: Array comparison
    ... pointers are only going to compare their object pointers ... pointers and not the contents of the strings. ... things like array of Char, bytes, words, dword, int64's etc.. ...
    (alt.comp.lang.borland-delphi)
  • Re: Questions regarding specialized malloc()/free() replacements
    ... that range *must* compare not-equal. ... Equal, not equal, sure, but the others require that the pointers be ... | or both point one past the last element of the same array object, ... | and pointers to array elements with larger subscript values compare ...
    (comp.lang.c)
  • Re: Differance between Array and Pointers
    ... Well, except that arrays tend to be much larger than pointers, and the ... An array is a contiguous region of memory containing N elements of M ... indexing vs. a loop). ...
    (comp.arch.embedded)
  • [RFCv2][PATCH] flexible array implementation
    ... I call it a flexible array. ... storage for pointers to the second level. ... all locking must be provided by the caller. ... make sure to pass in &ptr instead of ptr. ...
    (Linux-Kernel)