diff options
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; +} |