Add test program for valgrind.

This commit is contained in:
Ernie Pasveer
2022-10-16 18:17:24 -05:00
parent 55613f9e2c
commit 6d57c8434d
3 changed files with 51 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
hellovalgrind
+10
View File
@@ -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
+40
View File
@@ -0,0 +1,40 @@
#include <stdio.h>
#include <stdlib.h>
// 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;
}