Author Topic: A very basic question on 'C' variables  (Read 1961 times)

0 Members and 1 Guest are viewing this topic.

Offline InvictaTopic starter

  • Full Member
  • ***
  • Posts: 65
  • Helpful? 1
A very basic question on 'C' variables
« 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
« Last Edit: September 13, 2010, 06:07:59 PM by Invicta »

Offline knossos

  • Robot Overlord
  • ****
  • Posts: 278
  • Helpful? 14
Re: A very basic question on 'C' variables
« Reply #1 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.
"Never regret thy fall,
O Icarus of the fearless flight
For the greatest tragedy of them all
Is never to feel the burning light."
 
— Oscar Wilde

Offline madsci1016

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
    • Personal Website
Re: A very basic question on 'C' variables
« Reply #2 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.

Offline madsci1016

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
    • Personal Website
Re: A very basic question on 'C' variables
« Reply #3 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

Offline Razor Concepts

  • Supreme Robot
  • *****
  • Posts: 1,856
  • Helpful? 53
    • RazorConcepts
Re: A very basic question on 'C' variables
« Reply #4 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.

Offline InvictaTopic starter

  • Full Member
  • ***
  • Posts: 65
  • Helpful? 1
Re: A very basic question on 'C' variables
« Reply #5 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

Once again

Thank you.     p.s. madsci1016 +1 helpful
« Last Edit: September 14, 2010, 11:01:48 AM by Invicta »

Offline knossos

  • Robot Overlord
  • ****
  • Posts: 278
  • Helpful? 14
Re: A very basic question on 'C' variables
« Reply #6 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.
"Never regret thy fall,
O Icarus of the fearless flight
For the greatest tragedy of them all
Is never to feel the burning light."
 
— Oscar Wilde

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,165
  • Helpful? 111
    • Webbot stuff
Re: A very basic question on 'C' variables
« Reply #7 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.




Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline madsci1016

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
    • Personal Website
Re: A very basic question on 'C' variables
« Reply #8 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!

Offline knossos

  • Robot Overlord
  • ****
  • Posts: 278
  • Helpful? 14
Re: A very basic question on 'C' variables
« Reply #9 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. :)
"Never regret thy fall,
O Icarus of the fearless flight
For the greatest tragedy of them all
Is never to feel the burning light."
 
— Oscar Wilde

 


Get Your Ad Here

data_list