Skip to content

Instantly share code, notes, and snippets.

@arjunmayilvaganan
Created December 22, 2019 16:18
Show Gist options
  • Save arjunmayilvaganan/425edd43f1dd5054d6cfe2f13660e75d to your computer and use it in GitHub Desktop.
Save arjunmayilvaganan/425edd43f1dd5054d6cfe2f13660e75d to your computer and use it in GitHub Desktop.
C++ (adaptable to C) program to determine the stack (memory) direction
#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