1

stack is increasing or decreasing using C program ?

Vishwanath Dalvi
  • 35,388
  • 41
  • 123
  • 155

6 Answers6

7

Right, in C usually variables in function scope are realized by means of a stack. But this model is not imposed by the C standard, a compiler could realize this any way it pleases. The word "stack" isn't even mentioned in the standard, and even less if it is in- or decreasing. You should never try to work with assumptions about that.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
2

False dichotomy. There are plenty of options other than increasing or decreasing, one of which is that each function call performs the equivalent of malloc to obtain memory for the callee's automatic storage, calls the callee, and performs the equivalent of free after it returns. A more sophisticated version of this would allocate large runs of "stack" at a time and only allocate more when it's about to be exhausted.

I would call both of those very bad designs on modern machines with virtual memory, but they might make sense when implementing a multiprocess operating system on MMU-less microprocessors where reserving a range of memory for the stack in each process would waste a lot of address space.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
1

How about:

int stack_direction(void *pointer_to_local)
{
     int other_local;

     return (&other_local > pointer_to_local) ? 1 : -1;
}

...
int local;
printf("direction: %i", stack_direction(&local);

So you're comparing the address of a variable at one location on the call stack with one at an outer location.

Tommy
  • 99,986
  • 12
  • 185
  • 204
  • 2
    To be safe, should turn off compiler optimisation so that it doesn't get inlined. – Dipstick Feb 13 '11 at 13:20
  • "&other_local > pointer_to_local" results in undefined behavior. – sigjuice Feb 25 '14 at 20:24
  • @sigjuice as noted elsewhere, whether C even uses a stack isn't defined. If the author is willing to assume a stack and make a best guess about direction then I think this is the best guessing code. All of which I should have said in the answer originally. – Tommy Feb 25 '14 at 21:12
0

EDIT

Read the comments. It doesn't seem to be possible to determine the stack direction using my method.

END EDIT

Declare an array variable on the stack and compare the addresses of consecutive elements.

#include <stdio.h>
#include <stdlib.h>
int
main(void)
{
    char buf[16];
    printf("&buf[0]: %x\n&buf[1]: %x\n", &buf[0], &buf[1]);  
    return 0;
}

The output is:

misha@misha-K42Jr:~/Desktop/stackoverflow$ ./a.out 
&buf[0]: d1149980
&buf[1]: d1149981

So the stack is growing down, as expected.

mpenkov
  • 21,621
  • 10
  • 84
  • 126
  • 1
    The variables can be possibly reordered by the compiler, so a slightly better idea would be to compare a stack variable from a function and a nested function. – Vlad Feb 13 '11 at 13:03
  • You have a point, but will the compiler ever reorder elements **within** an array? I wouldn't think so. – mpenkov Feb 13 '11 at 13:05
  • 1
    Pointer arithmetic requires that elements within an array come after one another. If you declare a 16 byte array, that'll either add 16 bytes to the stack pointer or subtract them but the array will still start in one place and go upwards to its end. – Tommy Feb 13 '11 at 13:08
  • 3
    The compiler could still have the stack grow in one direction and have array elements indexed the other way round. I don't see why one implies the other. – Jens Gustedt Feb 13 '11 at 13:09
  • @Jens Gustedt: Is there a way to do it, then? You could try using nested functions like @Vlad suggested, but the compiler could inline that function and re-order the variables. – mpenkov Feb 13 '11 at 13:13
  • Agree with Jens Gustedt - how is the address of elements in an array in anyway related to whether the stack grows up or down? – Dipstick Feb 13 '11 at 13:14
  • @misha: theoretically, there is no sure way, as anything is not guaranteed to be on stack. Practically, you can set the optimization level of the compiler to minimum, so the functions won't be inlined. Or just inspect the assembler output to look up what happens with the stack during the call or stack cleanup after the call. – Vlad Feb 13 '11 at 13:15
  • Thanks guys. I stand corrected -- I updated the answer. I'd delete it, but these comments are probably worth reading. – mpenkov Feb 13 '11 at 13:22
0

If you only like to know if the stack has been changed you can keep the last inserted object to the stack, peek at the top of it and compare the two.

Cu7l4ss
  • 556
  • 1
  • 8
  • 19
0

You can also monitor ESP register with inline assembly. ESP register holds address to unallocated stack. So if something is pushed to stack - ESP decreases and if pop'ed - ESP increases. (There are other commands which modifies stack, for example function call/return).

For example what is going on with stack when we try to compute recursive function such as Fibonacci Number (Visual Studio):

#include <stdio.h>

int FibonacciNumber(int n) {
    int stackpointer = 0;
    __asm {
        mov stackpointer, esp
    }
    printf("stack pointer: %i\n", stackpointer);

    if (n < 2)
        return n;
    else
        return FibonacciNumber(n-1) + FibonacciNumber(n-2);
}

int main () {
    FibonacciNumber(10);
    return 0;
}
Agnius Vasiliauskas
  • 10,935
  • 5
  • 50
  • 70