From 6f645bed54bd328d8a4075aad498892b0164c713 Mon Sep 17 00:00:00 2001 From: Matthew Sotoudeh Date: Thu, 27 Jul 2023 15:24:42 -0700 Subject: support arrays of pointers in refcounting --- python/examples/refcounting/test.c | 48 +++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 19 deletions(-) (limited to 'python/examples/refcounting/test.c') diff --git a/python/examples/refcounting/test.c b/python/examples/refcounting/test.c index 212bb8b..cd53e8b 100644 --- a/python/examples/refcounting/test.c +++ b/python/examples/refcounting/test.c @@ -2,33 +2,43 @@ #include int *foo(void) { - int *ptr = 0; - for (int i = 0; i < 5; i++) { + 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 = calloc(1, sizeof(*ptr)); - *ptr = 5; - return ptr; + ptr = calloc(1, sizeof(*ptr)); + *ptr = 5; + return ptr; } struct bar { - int x; - int *y; + 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; + struct bar bar; + bar.x = 5; + bar.y = calloc(1, sizeof(int)); + *bar.y = bar.x; + return bar.y; +} + +int *arrays(void) { + int *foo[3]; + foo[0] = calloc(1, sizeof(int)); + foo[1] = calloc(1, sizeof(int)); + foo[2] = calloc(1, sizeof(int)); + *(foo[1]) = 123; + return foo[1]; } int main(void) { - int *x = foo(); - printf("Result of foo(): %d\n", *x); - x = bar(); - printf("Result of bar(): %d\n", *x); - return 0; + int *x = foo(); + printf("Result of foo(): %d\n", *x); + x = bar(); + printf("Result of bar(): %d\n", *x); + x = arrays(); + printf("Result of arrays(): %d\n", *x); + return 0; } -- cgit v1.2.3