Embedded C and static variable auto initialisation to zero

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Taken from the Codevision compiler manual:

If not specifically initialised, static variables are automatically set to 0 at program startup.

 

However, Googling about C and static variables, I came across this:

First of all in ISO C (ANSI C), all static and global variables must be initialized before the program starts.

 

So is it basically compiler dependent, or in embedded C are static variables always automatically set to zero if the programmer give them an initial value.

 

That even raises more questions, such as is "embedded C" some sort of standard, or just a description of C when used in a hardware embedded environment as opposed to a general PC.

 

And then can you use different C standards in an embedded C compiler, thus changing the rules of what code is produced.

 

Keith.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Where's the second quote from?

Note: "before the program starts", which I can interpret the same as the first quote: "they are initialized by the startup code."  (Ie the second quote is talking about requirements for the startup code, not for the user program...)

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

westfw wrote:

Where's the second quote from?

Note: "before the program starts", which I can interpret the same as the first quote: "they are initialized by the startup code."  (Ie the second quote is talking about requirements for the startup code, not for the user program...)

 

 

https://www.programmersheaven.co...

Do we have another case of more "accurate" information on the web ??

 

Keith. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Artandsparks wrote:
Do we have another case of more "accurate" information on the web ??
The Embedded C standard :

ISO/IEC JTC1/SC22/WG14 - C

http://www.open-std.org/JTC1/SC22/WG14/

...

TR 18037: Embedded C

...

"Dare to be naïve." - Buckminster Fuller

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Longer version of the quote:

First of all in ISO C (ANSI C), all static and global variables [italic]must[/italic] be initialized before the program starts. If the programmer didn't do this explicitly, then the compiler must set them to zero.  If the compiler doesn't do this, it doesn't follow ISO C. Exactly [italic]how[/italic] the variables are initialized is however unspecified by the standard.

And then codevision says "we do this."  So Codevision follows ISO C.  Sounds like "violent agreement" to me...

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Statics are just hidden entries in .bss so they get zeroed by the CRT just like all .bss variables. In fact in CV I believe it just floods all RAM with 0x00 and then copies .data on top.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

With gcc you can pretty easily verify in the assembly output of the compiler if it is conform to standards and does what you want.

Save output, add an extra static var, recompile, compare with grep or meld, analyse.

Will probably take ten minutes.

 

Paul van der Hoeven.
Bunch of old projects with AVR's:
http://www.hoevendesign.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Paulvdh wrote:

With gcc you can ...

But OP said CodeVision.  And it is no harder to do the described procedure than with Gcc. [IMO/IME -- perhaps you have counters to that?]

 

Anyway, the mainstream C toolchains for AVR8 are pretty mature.  They are going to be very close to conforming, if not exact -- if you don't use extensions that might make AVR8 work easier or "better".  And indeed GCC has the C99 and other qualifiers to enforce a particular level/version/whatever.

 

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

westfw wrote:
Sounds like "violent agreement" to me...

Indeed!

 

The more common question is how to stop this standard behaviour from happening - generally in order to preserve data over a reset ...

 

 (and any decent embedded compiler will, indeed, have some way to do that).