473,378 Members | 1,439 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,378 software developers and data experts.

Reassigning an array of a class.

MS
Hi,

In my Genetic Algorithm program, I have a class called Genome. Another
class called GA has a class variable called 'population' which is an array
of type Genome.

One of the methods in GA, called CreateNextGeneration(), creates a local
array of class Genome called 'nextGeneration', and at the end of that
method I want the class variable 'population' to hold the array
'nextGeneration'.

Is it as simple as?

population = nextGeneration;

At the moment class Genome contains a copy method, and I have a loop which
copies all the Genomes in 'nextGeneration' to 'population' (the 2 arrays
are always the same size). This is obviously ineffecient, but I was
worried about what memory and garbage collection overheads there would be
it I used the line:

population = nextGeneration;

Should using this line be ok?

Many thanks,

MS
Jul 18 '05 #1
4 4331


MS wrote:
Hi,

In my Genetic Algorithm program, I have a class called Genome. Another
class called GA has a class variable called 'population' which is an array
of type Genome.

One of the methods in GA, called CreateNextGeneration(), creates a local
array of class Genome called 'nextGeneration', and at the end of that
method I want the class variable 'population' to hold the array
'nextGeneration'.

Is it as simple as?

population = nextGeneration;

At the moment class Genome contains a copy method, and I have a loop which
copies all the Genomes in 'nextGeneration' to 'population' (the 2 arrays
are always the same size). This is obviously ineffecient, but I was
worried about what memory and garbage collection overheads there would be
it I used the line:

population = nextGeneration;

Should using this line be ok?


Yes. Neither `population' nor `nextGeneration' is
actually an array; both are references to array objects.
Here's a crude schematic:

population -> [a,b,c]

nextGeneration -> [x,y,z]

When you execute `population = nextGeneration' you redirect
the upper arrow to point to the lower array object. If there
are no other references to the upper array, it becomes
eligible for garbage collection.

--
Er*********@sun.com

Jul 18 '05 #2
MS
> Yes. Neither `population' nor `nextGeneration' is
actually an array; both are references to array objects.
Here's a crude schematic:

population -> [a,b,c]

nextGeneration -> [x,y,z]

When you execute `population = nextGeneration' you redirect
the upper arrow to point to the lower array object. If there
are no other references to the upper array, it becomes
eligible for garbage collection.


Many thanks Eric.
Jul 18 '05 #3
Eric Sosman wrote:


MS wrote:
Hi,

In my Genetic Algorithm program, I have a class called Genome. Another
class called GA has a class variable called 'population' which is an array of type Genome.

One of the methods in GA, called CreateNextGeneration(), creates a local
array of class Genome called 'nextGeneration', and at the end of that
method I want the class variable 'population' to hold the array
'nextGeneration'.

Is it as simple as?

population = nextGeneration;

At the moment class Genome contains a copy method, and I have a loop which copies all the Genomes in 'nextGeneration' to 'population' (the 2 arrays
are always the same size). This is obviously ineffecient, but I was
worried about what memory and garbage collection overheads there would be
it I used the line:

population = nextGeneration;

Should using this line be ok?


Yes. Neither `population' nor `nextGeneration' is
actually an array; both are references to array objects.
Here's a crude schematic:

population -> [a,b,c]

nextGeneration -> [x,y,z]

When you execute `population = nextGeneration' you redirect
the upper arrow to point to the lower array object. If there
are no other references to the upper array, it becomes
eligible for garbage collection.


One important point though, is that after 'population = nextGeneration' both
population and nextGeneration refer to the *same* array.

If your CreateNextGeneration() method simply "populates" the array
nextGeneration, then you will also change the data which population refers
to. You need to make sure that CreateNextGeneration() creates a new array.

--
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : nm*@ion.le.ac.uk
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
Jul 18 '05 #4
MS
>> Yes. Neither `population' nor `nextGeneration' is
actually an array; both are references to array objects.
Here's a crude schematic:

population -> [a,b,c]

nextGeneration -> [x,y,z]

When you execute `population = nextGeneration' you redirect
the upper arrow to point to the lower array object. If there
are no other references to the upper array, it becomes
eligible for garbage collection.

One important point though, is that after 'population = nextGeneration' both
population and nextGeneration refer to the *same* array.

If your CreateNextGeneration() method simply "populates" the array
nextGeneration, then you will also change the data which population refers
to. You need to make sure that CreateNextGeneration() creates a new array.


Thanks.

CreateNextGeneration() does create a new local array, nextGeneration, and
fills it with new Genomes. It does some stuff and then finally reassigns
population: 'population = nextGegeration;'.

Cheers,

MS
Jul 18 '05 #5

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

Similar topics

9
by: Ken Godee | last post by:
I'm using the python Queue module, but since I can not find anyway to clear the Queue, I thought I would try to reassign ie...... q1 = Queue.Queue() q1.put('test1') q1.put('test2') q1.qsize()...
2
by: Joost Ronkes Agerbeek | last post by:
Is it legal to reassign an auto_ptr to another auto_ptr that is a member of the object the auto_ptr is holding before the reassignment? (Confused yet? :-p) #include <memory> class Node {...
6
by: Buddy Ackerman | last post by:
I created a simple class: Public Class MyTestClass Public Test() As String End Class I tried to assign some values to the array Test() and display them like this:
4
by: songkv | last post by:
Hi, I am trying to reassign an array of char to a string literal by calling a function. In the function I use pointer-to-pointer since I want to reassign the "string array pointer" to the string...
20
by: bhalicki | last post by:
Hi all, In the following code I am trying to change the contents of a string: int main() { char *string="testing"; rename(string); return 0;
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
6
by: woger151 | last post by:
If I write $x = new some_class($init_data1); and then $x = new some_class($init_data2); does the first object (constructed with $init_data1) get destroyed, or do I have to call unset() for...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
6
by: Tobe | last post by:
Hi, Here's an example of something that feels like it should be OK but does in fact produce a segfault on every compiler I've tried (VC2005, g ++ 4.1.2/Linux, g++ 3.4.4/Cygwin). The line marked...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.