On Mon, Jun 20, 2011 at 11:25, Andrew W. Nosenko
<[email protected]> wrote:
> On Sun, Jun 19, 2011 at 22:03, Andy Wingo <[email protected]> wrote:
>> Hi,
>>
>> On Sat 18 Jun 2011 22:25, "Andrew W. Nosenko" <[email protected]>
>> writes:
>>
>>> On Fri, Jun 17, 2011 at 13:21, Andy Wingo <[email protected]> wrote:
>>>> wingo@badger:/tmp$ cat foo.c
>>>> int
>>>> find_stack_direction ()
>>>> {
>>>> static char *addr = 0;
>>>
>>> Try to rewrite this line as
>>> volatile static char *addr = 0;
>>> It should help.
>>
>> It didn't, unfortunately.
>
> You are right. I marked the underlying char as volatile instead of
> the pointer itself.
> The proper version seems
> static char* volatile addr;
> but ATM I have no compiler around for verify that.
>
The
static char* volatile addr;
doesn't help also.
Gcc continues to think that it has rights to inline the inner
find_stack_direction() and messes the check as consequence.
Solution: make Gcc unable to inline the function call.
One of possible ways to achieve it: call the inner through volatile pointer.
typedef int (*func_t)();
int find_stack_direction ();
volatile func_t f = &find_stack_direction;
int
find_stack_direction ()
{
static char *addr = 0;
auto char dummy;
if (addr == 0)
{
addr = &dummy;
return f();
}
else
return (&dummy > addr) ? 1 : -1;
}
int
main ()
{
int r = find_stack_direction ();
return r < 0;
}
It's not the single way, but just first that come to minds.
--
Andrew W. Nosenko <[email protected]>