Skip to content

Instantly share code, notes, and snippets.

@sitano
Last active November 5, 2017 13:55
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 sitano/0372e516aadf7bbecabc33f0b6516704 to your computer and use it in GitHub Desktop.
Save sitano/0372e516aadf7bbecabc33f0b6516704 to your computer and use it in GitHub Desktop.
undefined behavior with realloc
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int* p = (int *)malloc(sizeof(int));
int* q = (int *)realloc(p, sizeof(int));
*p = 1;
*q = 2;
if (p == q)
{
printf("%d %d\n", *p, *q);
}
return 0;
}
@sitano
Copy link
Author

sitano commented Nov 5, 2017

$ clang -O3 -Wall -S -emit-llvm foo.c; cat ./foo.ll
$ clang -O3 -S -mllvm --x86-asm-syntax=att foo.c; cat ./foo.s
$ clang -O3 -Wall foo.c ; ./a.out
1 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment