I'm still curious about what you meant in posting the above link.
There is one link to source code on that page and that source code uses the name "top" in a conceptually similar way to what is probably in the program you are debugging. But neither the bug you just described
top = malloc(sizeof (top));
nor the strange Valgrind symptom you described earlier
correspond to anything in that source code.
Now that you posted the source code of the bug, it was easy to identify your other thread, containing a better discussion of that bug:
Looking at just your post directly above this (before finding your other thread) I was tempted to make the same suggestion as appears at the bottom of the reply you already got here:
That can be a cleaner approach in complicated projects, because it avoids putting information, (the struct name) that might be changed in subsequent program revisions, in a place where the compiler could not see the bug if you made such a change in one place but not the other.
You should understand sizeof(*top)
is not computed at run time and is not the size of the actual object top
points to (which might be NULL). It is computed at compile time and is the size of the kind of object that top
should point to.
is the size of the pointer, not the size of what it does and/or should point to.