InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search
  • Home
  • > Tutorials
  • > Linux

How to find recursion stack growth direction using C code

By admin | Last updated on Jan 27, 2016

In a Linux (or other Operating System) process when a subroutine is called, the memory for local variables comes from stack area of the process. Any dynamically allocated memory (using malloc, new, etc.) comes from the heap area of the process. During recursion local memory is allocated from stack area during function call and get cleared when the function execution is done.

Some more information can be obtained from the Call Stack wikipedia page.

The memory is being represented with lowest address being at the bottom and highest being at the top. Here are the steps to find the direction of stack growth in recursion using a quick C code. You can also download the code from github – process stack growth direction.

  1. Create a file main.c with the following code:
    #include <stdio.h>
    
    void test_stack_growth_direction(recursion_depth) {
      int local_int1;
      printf("%p\n", &local_int1);
      if (recursion_depth < 10) {
        test_stack_growth_direction(recursion_depth + 1);
      }
    }
    
    main () {
      test_stack_growth_direction(0);
    }
  2. Compile the program using gcc and Run it. (This can be done on Mac or Linux).
    gcc main.c
    ./a.out
  3. Here is the output on Mac (Mac OX X, Version 10.7.5, Processor 2.4 GHz Intel Code i5, Memory 4 GB, 1333 MHz, DDR3)
    0x7fff6e9e19ac
    0x7fff6f9e89a8
    0x7fff6f9e8988
    0x7fff6f9e8968
    0x7fff6f9e8948
    0x7fff6f9e8928
    0x7fff6f9e8908
    0x7fff6f9e88e8
    0x7fff6f9e88c8
    0x7fff6f9e88a8
    0x7fff6f9e8888
    0x7fff6f9e8868
  4. Here is the outcome on Ubuntu Linux (Cpu Intel(R) Xeon(R) CPU)
    0x7ffffeec790c
    0x7ffffeec78dc
    0x7ffffeec78ac
    0x7ffffeec787c
    0x7ffffeec784c
    0x7ffffeec781c
    0x7ffffeec77ec
    0x7ffffeec77bc
    0x7ffffeec778c
    0x7ffffeec775c
    0x7ffffeec772c

The stack is growing downwards on these specific setups as memory addresses are reducing. This depends on the architecture of the system and may have different behavior for other architectures.

Suggested posts:

  1. Javascript – Number function
  2. CSS animation-iteration-count
  3. Impact on LC_ALL on Linux sort
  4. Vim mini cheat sheet – handy commands reference
  5. Use x2go to access remote Ubuntu Linux
  6. CSS flex-wrap
  7. Javascript – create array from 0 to N-1 in nodejs
  8. How to write custom php in wordpress
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Interview Questions, Linux, Tutorials
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress