diff options
author | Matthew Sotoudeh <matthew@masot.net> | 2023-07-27 14:26:33 -0700 |
---|---|---|
committer | Matthew Sotoudeh <matthew@masot.net> | 2023-07-27 14:26:33 -0700 |
commit | 2b8985608d33abaae7b201a008e292cbbe2167ef (patch) | |
tree | 58a76aaf7c7447988b1c78095480dc8bce5d2ab4 /python/examples/refcounting/test.c | |
parent | 578531395ecbabd8179e31520c2832ac7d6d3765 (diff) |
add automated refcounting pass
Diffstat (limited to 'python/examples/refcounting/test.c')
-rw-r--r-- | python/examples/refcounting/test.c | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/python/examples/refcounting/test.c b/python/examples/refcounting/test.c new file mode 100644 index 0000000..212bb8b --- /dev/null +++ b/python/examples/refcounting/test.c @@ -0,0 +1,34 @@ +#include <stdlib.h> +#include <stdio.h> + +int *foo(void) { + int *ptr = 0; + for (int i = 0; i < 5; i++) { + ptr = calloc(1, sizeof(*ptr)); + } + ptr = calloc(1, sizeof(*ptr)); + ptr = calloc(1, sizeof(*ptr)); + *ptr = 5; + return ptr; +} + +struct bar { + int x; + int *y; +}; + +int *bar(void) { + struct bar bar; + bar.x = 5; + bar.y = calloc(1, sizeof(int)); + *bar.y = bar.x; + return bar.y; +} + +int main(void) { + int *x = foo(); + printf("Result of foo(): %d\n", *x); + x = bar(); + printf("Result of bar(): %d\n", *x); + return 0; +} |