Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C
Is there a difference between
 
int test = 1;
while(...)
{
    test = 1;
}
and
while(...)
{
    int test = 1;
}
Thanks for your response!
Posted 9-Nov-12 3:16am
Edited 9-Nov-12 4:03am
v2
Comments
Albert Holguin at 20-Nov-12 14:32pm
   
Keep in mind that declaring a variable within a loop is illegal in some older C standards.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

There is! In the first form the variable is visible outside of the while loop, whilst in the second form the variable is only visible inside the while loop. It is called variable scope[^].
If you need the variable after the loop has exited, then you should use the first form. The second form should be used if the variable will not be needed after the while loop. Using variables declared outside of a loop without actually needing that value outside will add confusion. If the value of that variable is not needed outside then declare (and define) a variable inside the loop.
 
Regards,
 
— Manfred
  Permalink  
v3
Comments
Andi Vogel at 9-Nov-12 11:40am
   
Think there additionally is a difference in performance. If you do not always while running through the 'while' loop initialize and destroy the variable, but only fill it with new values this should be faster or?
Manfred R. Bihy at 9-Nov-12 11:57am
   
Optimizing compilers already do stuff like that, i.e. the variable is already local to the containing method, it is just the compiler that watches out that it will not be accessed from places where it is semantically out of scope. This means that using variables definitions within loops will not incur a penalty when you have a good optimizing compiler.
You'd have to decompile / disassemble your compiler output to see what how your compiler performs.
 
Cheers!
Philippe Mori at 9-Nov-12 18:44pm
   
For simple types, there should be no difference in performance. It will matters for complex type or if an expression is recomputed inside the loop when it need to be done only once.
CPallini at 12-Nov-12 8:16am
   
5.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

With the following code snippet the difference of performance between the two ways of variable declaration can be checked:
 
	long double sysTime1;
	long double time1;
	long double sysTime2;
	long double time2;
 
	sysTime1 = GetTickCount();
 
	// First way
	CString test;
	int i;
	for (i = 0; i < 1000000; i++)
		test.Format("Test%d", i+1);
 
	time1 = GetTickCount() - sysTime1;
 
	sysTime2 = GetTickCount();
 
	// Second way
	for (int i = 0; i < 1000000; i++)
	{
		CString test;
		test.Format("Test%d", i+1);
	}
 
	time2 = GetTickCount() - sysTime2;
 
	CString difference;
	difference.Format("Difference: %lf ms", time2-time1); 
 
	MessageBox(NULL, difference, "Performance Test", MB_OK);
The differences on my windows system are:
 
Compiled with /Od : ca. 700 ms
Compiled with /O2 : ca. 600 ms
Compiled with /Ox : ca. 550 ms
 
Conclusion:
 
In time critical functions the first way of declaration should be used.
 
Best regards
 
sja
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Yes There is difference
In first the varibale is accessible inside the loop and outside the loop.
In second the variable is only accessible inside the loop.
 
In First The Variable is only one time Declared.
But in loop the garbage collector destroy it and it will again and again
declared.
  Permalink  
Comments
Member 9586495 at 12-Nov-12 11:00am
   
The question is:
 
How is it possible to use the compact, modern and elegant way of "in-loop" declaration like
 
for (int i = 0; ...)
{
CString test1 = ...
CString test2 = ...
...
}
 
without loss of performance.
 
Any idea?
Legor at 14-Nov-12 7:11am
   
With a good compiler you wouldn't have to care about performance loss. It will not free and reallocate the memory if it is not needed.
TalalAslam at 13-Nov-12 5:22am
   
The Compact Way is not to declare variable in the loop
you can do this
CString test1=....
CString test2=....
for (int i =0;....)
{
test1 processing..
test2 processing...
}
in this way the test1 and test2 is declared only once and can also accessible out side the loop
but in your written way the test1 and test 2 will again and declared and garbage collector will destroy it each time so that performance will be decrease, also test1 and test2 will only accessible inside loop.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

have a good time dear firenad
 
at first code, the "test" variable is global for the "while" and after the "while" it will be and can be used and memory will keep it's value.
but in the second code, the "test" is local in the "while" and after the "while" it will not be used to code.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

int test = 1;
while(...)
{
    test = 1;
}
 
The above coding is good practice. Beacuse declaration of variable is done before the for loop, it make only one int variable and use the memory of one int(4 bytes).
 
while(...)
{
    int test = 1;
}
 

In this case suppose n iterations done in while loop. So create integer varibales n times. So the previous value is not considering and stack range increases.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

"In-loop" declaration of variables and objects in the way
 
for (int i...)
{
	CString test = ...
	...
}
 
fulfills the paradigms of object-oriented programming like encapsulation, data hiding and visibility.
 
The problem of wasting performance is on the compiler developing side and a solution can start with the following pseudocode:
 
if in "while", "for", "do-while" loops do
	if "first cycle" do
		initialize and create "in-loop" objects
		assignments and processing...
	end
	else if "last cycle" do
		assignments and processing...
		destroy "in-loop" objects, free memory etc.
	end
	else do
		assignments and processing...
	end
end
 
Best regards
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 435
1 Sergey Alexandrovich Kryukov 200
2 Yogesh Kumar Tyagi 180
3 Richard Deeming 180
4 RyanDev 115
0 Sergey Alexandrovich Kryukov 6,405
1 OriginalGriff 5,798
2 CPallini 2,473
3 Richard MacCutchan 1,647
4 Abhinav S 1,530


Advertise | Privacy | Mobile
Web02 | 2.8.140821.2 | Last Updated 19 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100