Page 1 of 1

vector.record() question

Posted: Mon Jan 30, 2012 11:44 am
by Prokopiou
Hello all,

I might be asking something that is obvious but my mind is stuck.
I found similar code in other forum questions that should be working as I was expecting it to work but I get results I don't expect.

Code: Select all

objref test1
objref test2
objref results_list
objref tmpvec 

proc record_results() { 
	results_list = new List()
	tmpvec = new Vector()

	forall {

    test1 = new Vector()
test2 = new Vector()


record_results() // This saves the internal voltage of each segment in a list
The two test vectors (test1 and test2) work as expected giving the time evolution of the two section centres they are recording.

The results_list I expected to have a list with all the time evolution of the sections in my model. Instead I do get a list that has the correct number of sections but it holds only the last section's recording.

more specifically, i have 55 sections and this list has 55 identical vectors and they all correspond to the last section.
Why is this? Instead of passing a value I pass a reference that gets updated every time resulting in the same ouput?

Re: vector.record() question

Posted: Mon Jan 30, 2012 11:03 pm
by ted
You're off to a good start--only a minor repair is needed.

The cause of the problem is that tmpvec is pointing to the very same Vector instance on every step of the forall statement. So List ends up full of copies of the same objref, each one pointing to the same instance of the Vector class, and that Vector records whatever the last Vector.record statement told it to record.

You want to revise your code so that it looks like this:

Code: Select all

objref results_list

proc record_results() { localobj tobj
  results_list = new List()
  forall {
    tobj = new Vector()
 . . .

Re: vector.record() question

Posted: Tue Jan 31, 2012 6:08 am
by Prokopiou
Hello Ted,

Thank you! The code runs as it is supposed to now. I tried reassigning the vector to a Null after the for loop finished so it didn't occur to me that it had to be in a loop.
Monday is by far the worst day to try and code :)

Thank you again,