Subject:
|
Re: local variables
|
Newsgroups:
|
lugnet.robotics.rcx.legos
|
Date:
|
Wed, 17 May 2000 23:29:19 GMT
|
Reply-To:
|
sjm@judgement.[spamcake]com
|
Viewed:
|
1432 times
|
| |
| |
Michael J Ash wrote:
>
> On Tue, 16 May 2000, Tobias Bende wrote:
>
> > I read that legOS only declared varibles the first time the program is run.
> > Does this mean I cannot use local variables. I have loops in different
> > functions using 'i' as looping variable, which is locally declared. Should I
> > avoid this?
>
> I think you're confusing the issue a little bit. LegOS only initializes
> GLOBAL variables when the program is loaded. So, for example, a global
> variable declared like:
>
> int foo = 4;
>
> Will only be initialized to 4 when the program is loaded. If you increment
> foo in your main function and don't reset it, foo will be 5 the next time
> you run the program. This also applies to static local variables in
> functions. Normal local variables are always taken care of properly each
> time the function is called, though. (If I'm mistaken here, please tell
> me! My programs would thank you! :)
This is completely true and is basically a feature of the C language.
Globals aren't technically "initialized" at all. They are just loaded
as part of the image. You could consider that they are initialized at
compile time. On a workstation OS the program is reloaded each you run
it and that is what most of us are used to. In LegOS programs are really
function calls with a little more complex calling scheme. They get
loaded
once and called many times.
It wouldn't be that hard to change this at the cost of a little code
and space. The linker allows you to put the global data in two places,
logical and physical. A startup stub could copy the data from one place
to the other, plus zeroing the bss. This is often done in embedded
systems although the physical location is usually ROM. I think the
benefit would be minimal for most applications. It is not that hard
to have an intialize function that gives global variables an initial
value.
Auto variables of course are allocated in temporary locations "on the
stack"
or in registers and are initialized every invovation of a function. This
again is a standard C feature. Nothing special for gcc-H8 or LegOS.
Except for the difference in when programs are loaded LegOS behaves
exactly like any other C environment. That is its power.
|
|
Message is in Reply To:
| | Re: local variables
|
| (...) I think you're confusing the issue a little bit. LegOS only initializes GLOBAL variables when the program is loaded. So, for example, a global variable declared like: int foo = 4; Will only be initialized to 4 when the program is loaded. If (...) (25 years ago, 17-May-00, to lugnet.robotics.rcx.legos)
|
3 Messages in This Thread:
- Entire Thread on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
|
|
|
|