473,414 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Simple question on declaring a variable

Dom
I should know this, but I don't. In the following, is an integer
being created with each iteration?

for (...)
{
int n = SomeFunction ();
}

Or should I do the following, even if I don't plan to use "n" outside
the loop?

int n;
for (...)
{
n = SomeFunction ();
}
Dec 18 '07 #1
4 1058
Hi,
--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
"Dom" <do********@gmail.comwrote in message
news:0e**********************************@d27g2000 prf.googlegroups.com...
>I should know this, but I don't. In the following, is an integer
being created with each iteration?
No, only 1 position in reserved in memory for the variable.

for (...)
{
int n = SomeFunction ();
}

Or should I do the following, even if I don't plan to use "n" outside
the loop?
You will have the same memory comsuption in both cases. but they are not the
same!
In this second case the variable can be referenced outside the for loop. In
the first case n only exist inside the loop.
int n;
for (...)
{
n = SomeFunction ();
}


Dec 18 '07 #2
In the general case it makes no difference and compiles to the same
thing. I would use the first syntax just to limit the scope.

Caveat: if the variable is "captured" inside an anonymous method (or
lambda in C# 3), then note that then lifetime and separation relates
to the code-block which declares it; but you don't seem to be doing
that here...

Marc
Dec 18 '07 #3
Dom wrote:
I should know this, but I don't. In the following, is an integer
being created with each iteration?

for (...)
{
int n = SomeFunction ();
}

Or should I do the following, even if I don't plan to use "n" outside
the loop?

int n;
for (...)
{
n = SomeFunction ();
}
It does not matter functionally, but I would recommend the first,
because it limits the scope of n.

Arne
Dec 22 '07 #4
Arne Vajhøj <ar**@vajhoej.dkwrote:

<snip>
It does not matter functionally, but I would recommend the first,
because it limits the scope of n.
Not disagreeing with you, but it's worth emphasising that as Marc said,
it *does* matter in more complex code where the variable is captured.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 22 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Basil Fawlty | last post by:
Hi everyone, I'm a newbie to C/C++ and I need some help with a minor C program. My assignment is to take two programs and mesh them together. The 1st program I wrote compiles and works fine. The...
2
by: TNGgroup | last post by:
Hi, a Simple Question, I have some code at my form, the code in the form is calling a certain module and execute some code, a certain variable is set to strTemp. but when de module is done. I...
1
by: Maarten Terlingen | last post by:
Sample: ClassA: ClassB ClassB: ClassC ClassA x = new Class C I want to know the declaring type of C. x.GetType() returns C x.GetType().BaseType returns B x.GetType().BaseType.BaseType...
5
by: Brett | last post by:
In a class, I have several Private subs. I declare an instance of the class such as: Dim MySelf as new Class1 within a private sub. The motive is to provide access to other subs within the...
6
by: Mark A. Sam | last post by:
Hello, I am using Visual Web Developer 2005 Express. I want to declare a varible, using Visual Basic as the language and can't get anywhere. For example Public Test1 as String I'll get en...
8
by: rendle | last post by:
I have a MSIL/performance question: Is there any difference between declaring a variable once and assigning to it multiple times, and declaring and assigning multiple times? For example: //...
8
by: SM | last post by:
I've always wonder if there is diference when declaring and initializing a varible inside/outside a loop. What's a better practice? Declaring and initializing variables inside a loop routine,...
13
by: aaragon | last post by:
Hi everyone, I just wanted to know if there is any difference in performance in declarating the variables in the beginning of a function or within for loops. For example: double test() {...
3
by: deejayquai | last post by:
Hello Simple one this I guess, but I'm quite stuck at the moment. I would like to update the records displayed in my listbox (lstStudents) using criteria selected from my combo (cboForm) in a...
30
by: galiorenye | last post by:
Hi, Given this code: A** ppA = new A*; A *pA = NULL; for(int i = 0; i < 10; ++i) { pA = ppA; //do something with pA
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.