1 #include <stdlib.h> 2 3 #ifndef alloca 4 #define alloca __builtin_alloca 5 #endif 6 7 static int foo()8foo() { 9 char *p; 10 char *p2; 11 12 if (0) { 13 p = alloca(16); 14 } 15 if (1) { 16 p2 = alloca(128); 17 } 18 strcpy(p2, "hmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm"); 19 return puts(p2); 20 } 21 22 static long long bar()23bar() { 24 char *p; 25 26 p = alloca(5); 27 strcpy(p, "hm"); 28 return 12955599955555ll; 29 } 30 31 static void check_mem_leak()32check_mem_leak() { 33 char *p = alloca(1000000); /* ~1mb */ 34 35 if (p == NULL) { 36 abort(); 37 } 38 } 39 40 int main()41main() { 42 char *p = alloca(128); 43 int i; 44 45 strcpy(p, "hellohellohellohello"); 46 puts(p); 47 p = malloc(128); 48 strcpy(p, "hmmmmmmmmm"); 49 puts(p); 50 free(p); 51 printf("%d\n", foo()); 52 printf("%lld\n", bar()); 53 for (i = 0; i < 10000; ++i) { 54 check_mem_leak(); 55 } 56 } 57 58