Created
September 15, 2018 22:14
-
-
Save rachtsingh/f2fe71d6f63361e416dc5bed408ec098 to your computer and use it in GitHub Desktop.
Trying to use stack memory after it's gone
This file contains hidden or 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 <stdio.h> | |
// void * return type means that it returns a naked pointer, which is just an address. | |
void *foo(void) { | |
long bar = 5; | |
printf("bar's address is: %p, holds %ld bytes\n", &bar, sizeof(long)); | |
long *ret = &bar; | |
return (void *) ret; // ret is a pointer to a long, but we just turn it into a regular address | |
// note that the previous line doesn't *do* anything, it just gets the compiler off our back | |
} | |
int main(void) { | |
void *ptr = foo(); | |
printf("about to print what's at %p\n", ptr); | |
long *l_ptr = (long *) ptr; | |
printf("%ld\n", *l_ptr); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment