From 2b8985608d33abaae7b201a008e292cbbe2167ef Mon Sep 17 00:00:00 2001 From: Matthew Sotoudeh Date: Thu, 27 Jul 2023 14:26:33 -0700 Subject: add automated refcounting pass --- python/examples/refcounting/test.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 python/examples/refcounting/test.c (limited to 'python/examples/refcounting/test.c') 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 +#include + +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; +} -- cgit v1.2.3