Skip to content

Instantly share code, notes, and snippets.

@markd2
Created January 6, 2019 22:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markd2/097ab91de4f7b28c339834eb26d7b81e to your computer and use it in GitHub Desktop.
Save markd2/097ab91de4f7b28c339834eb26d7b81e to your computer and use it in GitHub Desktop.
#import <stdio.h>
#import <stdlib.h>
// clang -g -Wall -o malloc malloc.c
int main(void) {
int *blah = malloc(sizeof(int));
printf("blah on alloc: %x\n", *blah);
*blah = 23;
printf("blah after assignment: %x\n", *blah);
free(blah);
blah = malloc(sizeof(int));
printf("blah after free and malloc: %x\n", *blah);
return 0;
}
/*
% uname -a
Darwin blah.local 17.7.0 Darwin Kernel Version 17.7.0: Thu Jun 21 22:53:14 PDT 2018; root:xn\
u-4570.71.2~1/RELEASE_X86_64 x86_64
% clang --version
Apple LLVM version 10.0.0 (clang-1000.11.45.5)
Target: x86_64-apple-darwin17.7.0
Thread model: posix
InstalledDir: /Applications/Xcode-10-1.app/Contents/Developer/Toolchains/XcodeDefault.xctool\
chain/usr/bin
% clang -g -Wall -o malloc malloc.c
% ./malloc
blah on alloc: 0
blah after assignment: 17
blah after free and malloc: 17
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment