Initialise Variables to a fixed number e.g 123

Moderator: Benj

Post Reply
medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Initialise Variables to a fixed number e.g 123

Post by medelec35 »

When a variable is is read before initialised with a value could this be variable be set at 123 instead of 0
With hardware, the uninitialised value could be a random number.
With Flowcode at the moment uninitialised variables are set to 0.
So if you forget to initialised before a routine like count=count+1 if count >4 then count = 0; exit.
Then with Flowcode simulator when run this will work. But if run on hardware will not work. So simulation can be different from real life.
If however you start Flowcode and uninitialised count is set at 123.
Not only can you see count is 123 therefore problem is variable must be uninitialised, but if simulator is ran, code will not run as expected like real hardware, making it easier to debug.
This problem I'm sure catches a quiet few people out.
Martin

User avatar
Steve
Matrix Staff
Posts: 3418
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: Initialise Variables to a fixed number e.g 123

Post by Steve »

Yes, we've thought about this issue before. But we've never resolved it fully. There are various options we could implement:
  1. Initialise all variables to zero in code
  2. Initialise all variables to a random number in the simulator
  3. Warn the user when their program is attempting to use an uninitialised variable in simulation
  4. Leave it as it is
Everything has pros and cons. And there may be additional suggestions.

Perhaps this post should initiate an open discussion as to what is the preferred approach...

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Initialise Variables to a fixed number e.g 123

Post by Benj »

My vote would be this...

In the variable manager when creating a new variable there is a tick box to enable initialization. If this is ticked then you are allowed to enter a default initial value. This is then also carried across onto the chip and programmed at the beginning of the program.

If the initialization is not used then Flowcode could maybe warn the user if the variable is ever read before it is written. The problem then comes when interrupts are active and the variable is being read in the interrupt.

OR

At the start of main we have something along the lines of a special initialization calculation icon containing all the variables. This then sets the initial value for all variables. There are also downsides with this as local variables may not get initialized correctly without a similar mechanism and there may be quite a hit on performance or memory usage.


Random initial numbers in the simulator may be the best option without having to implement any big changes to the way the variable manager currently works.

Any other suggestions?

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times
Contact:

Re: Initialise Variables to a fixed number e.g 123

Post by Spanish_dude »

I think the best thing to do is a warning. Those unintialized variables are always showed as warning in the gcc compiler

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Initialise Variables to a fixed number e.g 123

Post by medelec35 »

I will take each point in turn:
1. Although this will work, A method would be required to get user in the habit of correcting uninitialised variables that require writing to, before read.

2. I believe this will lead to confusion. Although it's the most realistic version to real life, the user will not know if the value of variable is caused by a calculation error or uninitialised variable.

3. This sounds the the best idea by far. But if it won't be necessary to initialised first, since variable is wrote before read for instance then would it still warn?
Would this be more difficult for Flowcode designer to implement?

4. That would continue to cause problems with people getting stuck and not knowing reason. The more safeguards (hence more user friendly) in place and the less people have to ask for help would be beneficial in my eyes.

As you can guess my choice is option 3 :P

Edit: posted at the same time as Ben and Nicolas, so not read their posts. Now I have:
Benj wrote:In the variable manager when creating a new variable there is a tick box to enable initialization. If this is ticked then you are allowed to enter a default initial value. This is then also carried across onto the chip and programmed at the beginning of the program.

If the initialization is not used then Flowcode could maybe warn the user if the variable is ever read before it is written. The problem then comes when interrupts are active and the variable is being read in the interrupt. ?
Sounds like a good idea to me.
But again how easy is it for the devloper to implement?
Its it possible to have input from the developer that has to implement the function please?
Since they would now how praticable the change would be.
Martin

User avatar
JonnyW
Posts: 1230
Joined: Fri Oct 29, 2010 9:13 am
Location: Matrix Multimedia Ltd
Has thanked: 63 times
Been thanked: 290 times
Contact:

Re: Initialise Variables to a fixed number e.g 123

Post by JonnyW »

Hi there. I'll be implementing this, and it is something we have given a bit of thought to.

Yes, option 3 would certainly be my preference, if only one was chosen.

What I would like to do is have an 'Initialise to...' field in the variable manager that it should be good practice to use. This would enable Flowcode to initialise in the most efficient place (as opposed to the start of main()). If, during simulation a variable is uninitialised then this can be picked up in simulation and the User can receive a warning.

Unfortunately it would be too impractical to warn reliably at some static time (like during code generation) as even most compilers don't get this right, so we would be limited to run-time (simulation) - but we should be able to pass the initialised status along with the variable even into sub-routines, so catch pretty reliably there.

Cheers,

Jonny

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Initialise Variables to a fixed number e.g 123

Post by medelec35 »

JonnyW wrote:
What I would like to do is have an 'Initialise to...' field in the variable manager that it should be good practice to use. This would enable Flowcode to initialise in the most efficient place (as opposed to the start of main()). If, during simulation a variable is uninitialised then this can be picked up in simulation and the User can receive a warning.
Hi Jonny, That sounds good to me. It would certainly prevent issues caused by uninitialised variables. Every little thing that's makes hardware run more smoothly is definitely worth implementing.
I only suggested fixing to 123, as I thought it would be the fastest and easiest solution. But I what your suggesting just above can be implemented, even better!
Spanish_dude wrote:I think the best thing to do is a warning. Those unintialized variables are always showed as warning in the gcc compiler
Looks like option 3 is going to be most popular choice :P

Martin
Martin

Post Reply