diff --git a/.gitignore b/.gitignore index 845cda6..cfef67e 100644 --- a/.gitignore +++ b/.gitignore @@ -53,3 +53,5 @@ dkms.conf # debug information files *.dwo + +/sc \ No newline at end of file diff --git a/run.sh b/run.sh new file mode 100644 index 0000000..ac5ff80 --- /dev/null +++ b/run.sh @@ -0,0 +1,3 @@ +gcc scope-count.c -o sc + +./sc \ No newline at end of file diff --git a/scope-count.c b/scope-count.c new file mode 100644 index 0000000..8d1969e --- /dev/null +++ b/scope-count.c @@ -0,0 +1,59 @@ +#include +#include + +struct ScopeCount +{ + int count; + int is_alias; + void *item; +}; + +int scope_count(void *item, int is_alias) +{ + struct ScopeCount sc; + sc.count = 0; + sc.is_alias = 0; + sc.item = item; + + sc.count++; + sc.is_alias = is_alias; + + return sc.count; +} + +void end_scope(struct ScopeCount *sc) +{ + if (sc->count == 0) + { + free(sc->item); + } + sc->count--; +} + +struct ScopeCount do_something() +{ + struct ScopeCount sc; + sc.count = 1; + sc.is_alias = 0; + + sc.item = malloc(sizeof(int)); + + *(int *)sc.item = 67; + + end_scope(&sc); + + return sc; +} + +int main() +{ + struct ScopeCount sc = do_something(); + + printf("value: %d\n", *(int *)sc.item); + + end_scope(&sc); + + printf("value: %d\n", *(int *)sc.item); + + return 0; +}