Re: Detect direction of stack growth

From: xarax (xarax_at_email.com)
Date: 12/27/04


Date: Mon, 27 Dec 2004 04:18:10 GMT


"Adam Warner" <usenet@consulting.net.nz> wrote in message
news:pan.2004.12.27.03.00.17.959974@consulting.net.nz...
> Hi Keith Thompson,
>
> > Adam Warner <usenet@consulting.net.nz> writes:
> >> Is this a (C99) portable way to detect whether the C stack grows
> >> upwards (1) or downwards (-1)?
> >
> > No. There is no portable way to do this. Applying a relational
> > operator to addresses of distinct objects (objects that are not
> > contained in some larger composite object) invokes undefined behavior.
>
> Can I cast the addresses of the objects on the stack to uint64_t
> (7.18.1.1) and use a relational operator upon those values? This type
> is defined for all conforming implementations with 64 bit integers.

You cannot get any meaningful information. Stack management
is implementation dependent. Some implementations grow down,
some grow up, some are segmented, some grow in either direction.

Forget about it.

> What practically portable method exists to perform pointer arithmetic via
> integers?

 Pointer arithmetic is only meaningful for addresses within a single object.

> Does one first perform an implementation-specific check upon the
> size of the address space?

A meaningless question.

> Can I practically rely upon the address space
> never being larger than 64 bits?

No. That presumes a linear address space. An address space
need not be linear. IBM mainframe architecture has non-linear
virtual spaces with 64-bit "addresses" and 25-bit "space designation".
Somewhat like trying to compare two complex numbers for greater-than
or less-than; non-sensical. Only equals or unequals has meaning when
comparing two objects that may not be the same object.



Relevant Pages

  • Re: fast scripting language
    ... One can only compare language implementations and one can always improve implementations, so one can never claim to be able to compare languages. ... So yes, non-commercial projects can use commercial software, in this case provided that they are not engaging in commerce. ...
    (comp.lang.misc)
  • Re: A simple simple comparison question
    ... CSS implementations may vary from browsers to browsers. ... As you can see, when test2 is a string, you have a ... Mozilla browser, whereas style is an object (not necessary to compare ...
    (comp.lang.javascript)
  • Re: Troubles in C land
    ... Christopher Hulbert wrote: ... > Most C implementations always round integer division to 0, ... > the index as a double and floor it. ...
    (comp.soft-sys.matlab)
  • Re: C/C++ string comparison
    ... [snip - debate over efficiency of operator== implementations] ... Yet calling strcmp seems it would be more ... string, and when a direct compare is different, then call a lexicographical ...
    (comp.lang.cpp)
  • Re: Very poor Lisp performance
    ... > How many implementations of Ocaml are there? ... > working on Ocaml is hammering that one version. ... > Ocaml to an open source language then choose a language for which there ... > If you want to compare Ocaml to a specific implementation of Lisp, ...
    (comp.lang.lisp)