summaryrefslogtreecommitdiff
path: root/python/examples/refcounting/test.c
diff options
context:
space:
mode:
Diffstat (limited to 'python/examples/refcounting/test.c')
-rw-r--r--python/examples/refcounting/test.c48
1 files changed, 29 insertions, 19 deletions
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 <stdio.h>
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;
}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback