Determine the Direction of Stack Growth

A simple way to determine the direction of stack growth is to compare the address of a function parameter with the address of a local variable in the function. Of course, the main difficulty here is to avoid compiler optimization, such as register variables, function inlining, and so on. So, the first attempt may look like the following function:

void stack_growth(char *function_parameter) {    char local;    if (&local > function_parameter)        printf("up
");    else        printf("down
");}

The above code can be called as follows:

char c = 'c'; stack_growth(&c);

This example can be improved by using volatile modifiers and changing scalar variables into arrays:

void stack_growth(volatile char *function_parameter) {    volatile char local [64];    if (&local [63] > function_parameter)        printf("up
");    else        printf("down
");}

and calling this function as follows:

volatile char c[64]; stack_growth(&c[63]);

Further improvements can be made using variable length parameter lists (va_arg), recursive calls that compare the address of a local variable in different recursive iterations (in this case, it is important to avoid tail recursion elimination), and so on. However, there is no portable, standard-compliant way to do this because:

  • Comparison of pointers is guaranteed valid only when the pointers refer to objects in the same array or to the location one past the end of the array.
  • The stack organization, as pointed out by W. Richard Stevens and others, may have different forms, depending on the build-in hardware support and other factors. Therefore, the best way to determine the direction of stack growth is to check the documentation and code it as a constant.

It is also possible to find out the direction of stack growth by noting that it is the opposite of the heap growth direction (stack and heap usually grow towards each other), but this method is not very reliable either. For example, take the following code:

char *m1 = (char *)malloc(2);char *m2 = (char *)malloc(2);

Whether m1 will be greater than m2 or not depends heavily on the memory allocation method.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS