Compilers detect unused variable within the scope of a function. However, I found there are many variables, defined inside a structure, which are never read (but may have been written many times). Is there any tool/analyzer or even compiler flags to detect such unused variables?
Example:
For example, in the following structure:
typedef struct jj_t {
int count;
int *list;
} jj;
Analyzer may find that count is never read anywhere in the code.
My analyze of my code, shows this frequently happens! This was my fault, but it maybe the common case for the applications developed by different users over the years. Removing these variable may significantly reduces memory usage. I just need a tool for detecting such variables and I will manually remove them.
Thanks in advance.
I can give one solution.
But:
The effort is probably much bigger than checking by hand. Almost every good IDE for programmers allows you to see all references to a given variable.
This probably won’t work in every case, you’ll need to specialize for some types.
This will be collected by single program run.
The idea is to wrap your data types. With such encapsulation you can count every read operation.
See:
And usage:
Instead of:
DO this:
It will results in:
Notice last line printed after
main(). You can collect this data to some external file.