View Single Post
  #7   Spotlight this post!  
Unread 21-01-2004, 11:11
WizardOfAz's Avatar
WizardOfAz WizardOfAz is offline
Lead Mentor
AKA: Bill Bennett
FRC #1011 (CRUSH)
Team Role: Engineer
 
Join Date: Mar 2003
Rookie Year: 2002
Location: Tucson, AZ
Posts: 101
WizardOfAz will become famous soon enough
Send a message via AIM to WizardOfAz
declared initial values (aka implicit initialization)

Quote:
Originally Posted by Mike Betts
You are doing an implicit initialization. Never a good idea in an embedded system.

Make it:

int InitFoo;

for your declaration and then

InitFoo = (100 * 5);

in your initialization routine.
I guess I'm going to disagree about the "never a good idea" comment. If the environment supports initialization, then the provided initialization mechanism will likely be more efficient (faster and smaller) than doing the initialization in your own initialization code.

I started wondering how C18 and the FRC runtime environment was handling this issue, since ANSI C both supports initialization and specifies that unitialized static data is set to zero. Here's what I found. If I've got it wrong, somebody tell me.

First, C18 ships with three versions of startup code:
c018.o which does no initialization of data and is onlyl about 1k bytes big
co18i.o which initializes static data to declared initial values, about 4k bytes
co18z.o which initializes static data to declared initial values, and zeros the rest of the static data, also about 4k bytes

Which one you use is determined by your linker script.

When you look there, you'll see that none of them are used if you're using the linker script shipped with IFI default code. Instead, the project includes ifi_startup.c, which in fact is mostly assembler embedded in a C source file. This file has the startup code, and you'll see that it does both clear memory to zero and also copy initialized data to ram. It does these both with block copies so the code is very tiny.

So at least in our FRC and EDURC environments, you can be assured that static declarations (data declared outside of a function) like this
int foo = 10;
int bar;

will result in foo being initially 10 and bar being initially zero. This initialization is done only once at power-on-reset, not each time the function is called.

If these declaration are inside a function, they are not static data. If they have declared initial values, the compiler generates code to store the initial value each time the function is called; If they have no initial value, then there is none. This is non-static data and the C18 compiler does nothing to make its value predictable. This does not violate the ANSI standard.
so in

int foobar() {
int foo = 10;
int bar;
...
}

foo will be set to 10 every time foobar is called, but bar will have no predictable initial value.

Bottom line is that this environment does initialization according to the standard. As long as you understand how static and non-static initialization differ, it's safe to use.

Bill