Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Persistent/global variable problem

Subject: Persistent/global variable problem

From: gudny

Date: 30 Apr, 2010 09:36:43

Message: 1 of 3

Hello everyone,
I have a very strange bug in my program. Basically, I have a function
that creates nodes, giving each of them a new number. The current node
is the one with the highest counter at each time. The counter is a
persistent variable withing the function (at first I had it global, in
an attempt to fix the error I changed it into a persistent variable-
which didn't help).
The problem is that sometimes, the parent has a higher counter then
the newest child! Here is the pseodocode:

function [] = ask(parent_nr....)
 persistent counter
 node_nr = counter;
 nodes(node_nr) = parent_nr;
 if ~(parent_nr<node_nr)
    parent_nr
 end
 if not certain
 ask(node_nr, ...)
 end

What is even stranger, is that if I debug and have a breakpoint in the
if loop, in front of parent_nr, the error does not occur. It only
occurs, when there is no breakpoint. The debugging, seems to effect
how the program is run, it is very strange. I hope someone has an idea
what might be going on, or how I can get around it.
cheers,
Gudny

Subject: Persistent/global variable problem

From: us

Date: 30 Apr, 2010 09:44:05

Message: 2 of 3

gudny <gudnyg@gmail.com> wrote in message <187fd61c-9f27-4d6c-a806-22ee4d0d5bcf@d39g2000yqa.googlegroups.com>...
> Hello everyone,
> I have a very strange bug in my program. Basically, I have a function
> that creates nodes, giving each of them a new number. The current node
> is the one with the highest counter at each time. The counter is a
> persistent variable withing the function (at first I had it global, in
> an attempt to fix the error I changed it into a persistent variable-
> which didn't help).
> The problem is that sometimes, the parent has a higher counter then
> the newest child! Here is the pseodocode:
>
> function [] = ask(parent_nr....)
> persistent counter
> node_nr = counter;
> nodes(node_nr) = parent_nr;
> if ~(parent_nr<node_nr)
> parent_nr
> end
> if not certain
> ask(node_nr, ...)
> end
>
> What is even stranger, is that if I debug and have a breakpoint in the
> if loop, in front of parent_nr, the error does not occur. It only
> occurs, when there is no breakpoint. The debugging, seems to effect
> how the program is run, it is very strange. I hope someone has an idea
> what might be going on, or how I can get around it.
> cheers,
> Gudny

what happens if - instead of setting break points - you put a

     disp(counter)

in your code(?)...

us

Subject: Persistent/global variable problem

From: gudny

Date: 6 May, 2010 09:38:13

Message: 3 of 3

On Apr 30, 11:44 am, "us " <u...@neurol.unizh.ch> wrote:
> gudny <gud...@gmail.com> wrote in message <187fd61c-9f27-4d6c-a806-22ee4d0d5...@d39g2000yqa.googlegroups.com>...
> > Hello everyone,
> > I have a very strange bug in my program. Basically, I have a function
> > that creates nodes, giving each of them a new number. The current node
> > is the one with the highest counter at each time. The counter is a
> > persistent variable withing the function (at first I had it global, in
> > an attempt to fix the error I changed it into a persistent variable-
> > which didn't help).
> > The problem is that sometimes, the parent has a higher counter then
> > the newest child! Here is the pseodocode:
>
> > function [] = ask(parent_nr....)
> >  persistent counter
> >  node_nr = counter;
> >  nodes(node_nr) = parent_nr;
> >  if ~(parent_nr<node_nr)
> >     parent_nr
> >  end
> >  if not certain
> >  ask(node_nr, ...)
> >  end
>
> > What is even stranger, is that if I debug and have a breakpoint in the
> > if loop, in front of parent_nr, the error does not occur. It only
> > occurs, when there is no breakpoint. The debugging, seems to effect
> > how the program is run, it is very strange. I hope someone has an idea
> > what might be going on, or how I can get around it.
> > cheers,
> > Gudny
>
> what happens if - instead of setting break points - you put a
>
>      disp(counter)
>
> in your code(?)...
>
> us

hi sorry, I didn't see the answer. Yes, that also makes the problem
disappear. That is what I did in the end, I added a display statement
within the for loop that checks that the parent has a lower counter
than the child. I find this very disturbing though, and think I will
change my code, so that instead of counting nodes, I will work with
node objects.
thank you very much for your answer.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us