Skip to content

Instantly share code, notes, and snippets.

@markd2
Created January 6, 2019 22:39
Show Gist options
  • Select an option

  • Save markd2/097ab91de4f7b28c339834eb26d7b81e to your computer and use it in GitHub Desktop.

Select an option

Save markd2/097ab91de4f7b28c339834eb26d7b81e to your computer and use it in GitHub Desktop.

Revisions

  1. Mark Dalrymple created this gist Jan 6, 2019.
    42 changes: 42 additions & 0 deletions malloc.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    #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
    */