diff --git a/tests/hellovalgrind/.gitignore b/tests/hellovalgrind/.gitignore new file mode 100644 index 0000000..399a4b7 --- /dev/null +++ b/tests/hellovalgrind/.gitignore @@ -0,0 +1 @@ +hellovalgrind diff --git a/tests/hellovalgrind/Makefile b/tests/hellovalgrind/Makefile new file mode 100644 index 0000000..6fd8ba9 --- /dev/null +++ b/tests/hellovalgrind/Makefile @@ -0,0 +1,10 @@ +.PHONY: all +all: hellovalgrind + +hellovalgrind: hellovalgrind.c + gcc -g -o hellovalgrind hellovalgrind.c + +.PHONY: clean +clean: + rm -f hellovalgrind hellovalgrind.o + diff --git a/tests/hellovalgrind/hellovalgrind.c b/tests/hellovalgrind/hellovalgrind.c new file mode 100644 index 0000000..b34e667 --- /dev/null +++ b/tests/hellovalgrind/hellovalgrind.c @@ -0,0 +1,40 @@ +#include +#include + +// https://developers.redhat.com/articles/2021/11/01/debug-memory-errors-valgrind-and-gdb#using_valgrind_and_gdb_together + +typedef struct foo { + int flag1; + int flag2; + int size; + int **buf; +} foo; + +void print_buf(struct foo *s) +{ + + printf("buf[0][0]: %d\n", s->buf[0][0]); + free(s->buf); +} + +void setup_foo(struct foo *s) +{ + s->flag2 = 2; + s->buf = malloc(20 * sizeof(int)); + for (int i = 0; i < 20; i++) + s->buf[i] = malloc(20 * sizeof(int)); +} + +int main(void) +{ + struct foo s; + + setup_foo(&s); + print_buf(&s); + + if (s.flag1 || s.flag2) + printf("hey\n"); + + return 0; +} +