Created
September 15, 2018 22:19
-
-
Save rachtsingh/1cb1551838c6a443ca26b5f55376f8f8 to your computer and use it in GitHub Desktop.
Trying to access heap memory after it's been free'd
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 <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