473,654 Members | 3,097 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1411

"ajit goel" <aj*******@wipr o.com> wrote in message
news:6e******** *************** ***@posting.goo gle.com...
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
40623
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* just created a new pointer to the original value.... For example I wanted to initialize a list of empty lists.... a=, , , , ] I thought there has to be a *really* easy way of doing it - after a
2
1094
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
3710
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 array of this structure of around 100 elements. I want to initialise all the 100 elements of this array (and the 255 elements too) to some specific value (a non-zero value). Is there any way of doing it without using loops?
4
1532
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 (identical) value for each object in an array of 'Foo's: public class Foo { public Foo(bool val) {
2
1949
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 could point out any problems with it would be much appreciated. Private Sub Login_Click() Dim name, password, found, loops found = 0 loops = 0 name = Range("Username").Value password = Range("Password").Value
107
4099
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 variables and hence possible bugs. Your thoughts on the above would be welcome (as an aside), but my main problem follows. Now I've recently upgraded to gcc 4, and I find I'm missing the compiler warnings I used to get on gcc 3 regarding...
2
1563
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() { // blah } public static ACIS Get_ACIS(string sACISCode) { // blah }
6
1198
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 defined in the module (each module will contain exactly 1 object, which is a subclass of 'Plugin') The closest I've come so far is with something like:
33
7162
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 array, so im inputting the dimensions of those myself. The problem is that the output array (C=A*B) has as many rows as A and as many columns as B. I would think of initialising C with:
0
8375
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8815
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8707
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8482
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8593
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6161
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2714
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.