From 891c8fbd0c825b6716225e778a8f013afb858d57 Mon Sep 17 00:00:00 2001 From: Andrew Clayton Date: Fri, 7 Mar 2025 00:07:25 +0000 Subject: Initial commit Signed-off-by: Andrew Clayton --- examples/pass-by-value.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 examples/pass-by-value.c (limited to 'examples/pass-by-value.c') diff --git a/examples/pass-by-value.c b/examples/pass-by-value.c new file mode 100644 index 0000000..c452591 --- /dev/null +++ b/examples/pass-by-value.c @@ -0,0 +1,41 @@ +#include +#include + +struct s { + int i; +}; + +static void fv(struct s *s) +{ + /* Do something with s */ + + free(s); + s = NULL; +} + +static void fr(struct s **s) +{ + /* Do something with s */ + + free(*s); + *s = NULL; +} + +int main(void) +{ + struct s *s; + + s = malloc(sizeof(*s)); + printf("s: %p\n", s); + fv(s); + printf("s: %p\n", s); + + printf("\n"); + + s = malloc(sizeof(*s)); + printf("s: %p\n", s); + fr(&s); + printf("s: %p\n", s); + + return 0; +} -- cgit