Created
December 22, 2019 16:18
-
-
Save arjunmayilvaganan/425edd43f1dd5054d6cfe2f13660e75d to your computer and use it in GitHub Desktop.
C++ (adaptable to C) program to determine the stack (memory) direction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
using namespace std; | |
static int find_stack_direction () | |
{ | |
static char *addr = 0; | |
auto char dummy; | |
if (addr == 0) | |
{ | |
addr = &dummy; | |
return find_stack_direction (); | |
} | |
else | |
{ | |
return ((&dummy > addr) ? 1 : -1); | |
} | |
} | |
int main() { | |
int d = find_stack_direction(); | |
cout<<"stack direction: "<<((d == 1) ? "upwards":"downwards")<<"\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment