Skip to content

Instantly share code, notes, and snippets.

@rachtsingh
Created September 15, 2018 22:19
Show Gist options
  • Save rachtsingh/1cb1551838c6a443ca26b5f55376f8f8 to your computer and use it in GitHub Desktop.
Save rachtsingh/1cb1551838c6a443ca26b5f55376f8f8 to your computer and use it in GitHub Desktop.
Trying to access heap memory after it's been free'd
#include <stdio.h>
#include <stdlib.h>
// void * return type means that it returns a naked pointer, which is just an address.
void *foo(void) {
long *bar_ptr = malloc(sizeof(long));
*bar_ptr = 5;
printf("The data is held at address: %p, holds %ld bytes\n", bar_ptr, sizeof(long));
free(bar_ptr);
return (void *) bar_ptr;
}
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