473,396 Members | 2,003 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,396 software developers and data experts.

initialising and creating objects in loops

Hi;

I have a question which respect to initialising and creating objects
in loops.Here are the 2 scenarios:

1.
################################################## #############################
for (int i=0;i<5;i++)
{
object a= new object();
// do some work on the object
}
################################################## #############################

or
2.
################################################## #############################

object a;

for (int i=0;i<5;i++)
{
a= new object();
// do some work on the object
}
################################################## #############################
Which would be more efficient and why??

Kind Regards;

Ajit Goel
Jul 21 '05 #1
1 1371

"ajit goel" <aj*******@wipro.com> wrote in message
news:6e**************************@posting.google.c om...
Hi;

I have a question which respect to initialising and creating objects
in loops.Here are the 2 scenarios:

1.
################################################## #############################
for (int i=0;i<5;i++)
{
object a= new object();
// do some work on the object
}
################################################## #############################

or
2.
################################################## #############################

object a;

for (int i=0;i<5;i++)
{
a= new object();
// do some work on the object
}
################################################## #############################
Which would be more efficient and why??


Neither. In either case you should end up with one local variable with 5
assignments to it. The difference is accessiblity. In the first example a is
only valid within teh for loop, in the second its valid within the entire
containing scope.
Jul 21 '05 #2

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

Similar topics

23
by: Fuzzyman | last post by:
Pythons internal 'pointers' system is certainly causing me a few headaches..... When I want to copy the contents of a variable I find it impossible to know whether I've copied the contents *or*...
2
by: Neal D. Becker | last post by:
What is the recommended way to create a sequence of objects? Assume the objects have a default constructor: class X: def __init__(self): something This doesn't work: a = 64*(X(),)
7
by: Nidhi | last post by:
Hi all, I dont know whether this question has been raised before or not. Nevertheless, here is my question... I have a structure of around 255 elements of the same type, say int. I have an...
4
by: Mike Windsor | last post by:
This seems like a fairly basic thing to want to to, but as far as I can tell, it's not possible. Can anyone help? I have a class 'Foo' and I want to call the constructor with a particular...
2
by: squidge1990 | last post by:
i have created a database using excel and i have to set a security level i have written all of the vba code correctly i think but it just does seem to work properly. here is the code below if anyone...
107
by: DaveC | last post by:
I always used to initialise variables at declaration, then a couple of colleagues started telling me it was bad practice and that the compiler should be left to spot the use of uninitilised...
2
by: mark4asp | last post by:
Q: Initialising and updating a class with only static members & database dependency I have a class with the following members: public static List<ACISACIS_List; static AssetClass() { //...
6
by: Dave Challis | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I'm trying to write some code which: 1. Finds all modules in a plugin directory 2. Imports those modules 3. Creates an instance of each object...
33
by: Adam Chapman | last post by:
Hi, Im trying to migrate from programming in Matlab over to C. Im trying to make a simple function to multiply one matrix by the other. I've realised that C can't determine the size of a 2d...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...

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.