Society of Robots - Robot Forum

Software => Software => Topic started by: Invicta on September 13, 2010, 06:05:56 PM

Title: A very basic question on 'C' variables
Post by: Invicta on September 13, 2010, 06:05:56 PM
If you do not assign a default value to say an integer or float Global variable what does the compiler assume the value to be? I have not been able to find any 'C' reference document that says anything about what the compiler assumes!

Code snippet:

// Global Variables for Euler angles
float roll;  // No default value assignment

-----------------------------------------------------

void Compass_Heading()
{
  float cos_roll;   // Local variable
  float sin_roll;   // Local variable

  cos_roll = cos(roll);   // I assume that for the first iteration roll is = 0.0
  sin_roll = sin(roll);   // I assume that for the first iteration roll is = 0.0

//Remainder of function goes here
}

Thanks for your help
Title: Re: A very basic question on 'C' variables
Post by: knossos on September 13, 2010, 06:22:00 PM
The number will not be zero unless it is explicitly declared as zero.  For proper programming you need to either explicitly declare a starting value or read in an initial value before using the variable.  Anything else and you are asking for problems.
Title: Re: A very basic question on 'C' variables
Post by: madsci1016 on September 13, 2010, 08:23:49 PM
The number will not be zero unless it is explicitly declared as zero.  For proper programming you need to either explicitly declare a starting value or read in an initial value before using the variable.  Anything else and you are asking for problems.

Yes and No. The C standard will guarantee all global variables will be initialized as 0 if not initialized as something else.

The same guarantee does not extend to variables initialized inside functions however.
Title: Re: A very basic question on 'C' variables
Post by: madsci1016 on September 13, 2010, 08:26:50 PM
any 'C' reference document that says anything about what the compiler assumes!

Here you go.

http://www.nongnu.org/avr-libc/user-manual/FAQ.html#faq_varinit (http://www.nongnu.org/avr-libc/user-manual/FAQ.html#faq_varinit)
Title: Re: A very basic question on 'C' variables
Post by: Razor Concepts on September 13, 2010, 08:34:59 PM
While the C standard might state that, it would be good to check with whatever software you are using to find the exact answer. I wrongly assumed that all globals were initialized to zero when working with MSP430s, and it did give me a bit of trouble.
Title: Re: A very basic question on 'C' variables
Post by: Invicta on September 14, 2010, 10:24:47 AM
Thanks Guys

Fantastic, exactly the answers I was looking for. With your guidance I also found this interesting discussion that mirrors your views:

http://www.embeddedrelated.com/usenet/embedded/show/28174-1.php (http://www.embeddedrelated.com/usenet/embedded/show/28174-1.php)

Once again

Thank you.     p.s. madsci1016 +1 helpful
Title: Re: A very basic question on 'C' variables
Post by: knossos on September 14, 2010, 04:09:51 PM
Yes and No. The C standard will guarantee all global variables will be initialized as 0 if not initialized as something else.

The same guarantee does not extend to variables initialized inside functions however.

I have come across times where I assumed the global would get initialized and ended up with headaches later.  Always better to be safe than sorry and declare it.
Title: Re: A very basic question on 'C' variables
Post by: Webbot on September 14, 2010, 04:43:27 PM
Using the gcc compiler (WinAVR) then all global variables are set to 0 before calling your main.

Some folk have said that you should initialise variables explicitly to avoid confusion. ie:
Code: [Select]
int myVar = 0;
In an 'ideal' world this is true - because you can read the code and know that the start value is 0.

However: doing this will actually make your 'program' bigger - ie it will need more flash memory !

Why?

Well all un-initialised variables are just lumped together into sequential bytes of RAM (called the BSS). The C compiler then inserts a 'for' loop to zap all of these to zero before calling your main since it knows how many there are and how big they are.

The compiler also lumps together all the variables that you give an initial value to. It has no choice but to store this list of initial values in program (flash) memory. It then has a similar 'for' loop, before your main, to copy all of these initial values out of your program memory and into the RAM.

So if you initialise global variables to a specific value then the variable will take up not only runtime RAM space but also the same amount of flash/program space to store the initial value.

Of course if you are using a large AVR then this may not be a problem.




Title: Re: A very basic question on 'C' variables
Post by: madsci1016 on September 14, 2010, 05:09:00 PM
Man, that's so much more clear then the FAQ section I linked. Now I understand it better!
Title: Re: A very basic question on 'C' variables
Post by: knossos on September 14, 2010, 06:36:01 PM
Very well said Webbot.  My experience is primarily computer programming.  The amount of memory used to declare one value (even a larger data structure) is typically insignificant when dealing with computers, particularly compared to potential programming pitfalls and portability of code.  I have to get back into the mindset of optimizing my code size. :)