c++ Unused variable

May 20, 2011 at 12:49pm
Hi,
I am working in large code base in c++(may be 8 million code). In My application I have seen thousands of unused variable and reported by g++ compiler but till now we did not clean up.I want to take intiative for cleaning
this varible but I need some info before working this issue.

Could anyone provide me info what will be the issue/disadvantage if we have thousands of unused variable. I searched into google but did not find any info.


I also seen into complier that they ignore warning but I believe we should treat warning as error. if possible could anyone provide me info what could be disaster if we ignore warning

What do you think guys we should effort to rectify this problem or it just
of waist effort





May 20, 2011 at 1:30pm

There will not be any disaster if you ignore the warning. Just that your binary will be big and will have some performance penalty when objects are constructed which are not used. Stack will be used unnecessary.

I suggest you comment all these and remove them. You must be using some version control tool as the code is big, so removing them would not be bad either.
May 22, 2011 at 12:29pm
Thanks for your input. Yes we are using version control system so it is much better if I remove them.

But can I get little more info about issue which may be arise if we contain unused variable.
it will help me to discuss this issue in proper way within the organization
May 22, 2011 at 12:31pm
it may be any paper or any url...
May 22, 2011 at 12:46pm
Just that your binary will be big and will have some performance penalty when objects are constructed which are not used. Stack will be used unnecessary.

Nah, just the fact that the compiler knows that the variables are unused (and thus can show the warning) almost automatically means that it will generate no code for them (except when the warnings refer to function parameters and the calls to that particular function are not inlined).

You can simply disable the warning with -Wno-unused-variable. However, this warning can often indicate actual errors in the code, so I wouldn't advise it. Then again, since you already have that many warnings of that type, it's probably hard to tell any new warnings apart from the old ones anyway.
May 22, 2011 at 3:36pm
ok Thanks for your input.. so it means compiler take care of unused variable and we does not pay more penalty if we do not remove unused variable except performance. The performance
issue will only occur if we have tons of unused variable.

Am I understanding is correct?
May 23, 2011 at 6:15am

@Arthar, that depends on the optimization of the compiler. do you mean,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class temp
{
public:

	temp()
	{
		std::cout << "temp" << std::endl;
	}
};

int main()
{
	temp t;
	temp t1;
	temp t3;
}


will print nothing?
Topic archived. No new replies allowed.