2014-09-19 22:03 GMT+08:00 Larry Evans <cppljevans@suddenlink.net>:
On 09/18/2014 10:02 PM, TONGARI J wrote:
> Hi,
>
> The problem had been discussed here:
> https://groups.google.com/forum/#!topic/boost-devel-archive/cl8B1oUPrVM
>
> But it's still not clear how to set the stack pointer portably, even after
> examining the code of Boost.Coroutine. For now, Boost.Context only supports
> downward stack, and Boost.Coroutine, which happened to be written by the
> same author, just assumes that the stack always grows downward, which is
> not really a portable implementation, if future Boost.Context supports
> upward stack, Boost.Coroutine has to be changed as well.

Several years ago, I had a look at the Boehm garbage collector:

http://www.hboehm.info/gc/

IIRC, it had to determine whether the stack grew upward or
downward.  I can't remember where in the code that was,
but I also vaguely remember reading (I think in the code
commets) that the method was not completely portable.

I guess something like below should work?

    #include <iostream>
    
    __attribute__((noinline)) bool is_upward_test(char* a)
    {
        char b[32];
        return a < b;
    }
    
    bool is_upward()
    {
        char a[32];
        return is_upward_test(a);
    }
    
    int main(int argc, char** argv) {
        std::cout << is_upward();
    return 0;
    }