Initialized integers.

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

I am using the GCC Compiler to program an ATMega32. When I declare an integer, it is initialized to zero. Who does this? Is it the compiler inserting a serialize command after the address has been assigned or does the ATMega32 handle this?

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

Quote:
Is it the compiler
Yep, all ram gets cleared..usually..
Your code should then initialise to values other than 0 anything that you need to be so.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Get any C book and it will be specified in gory detail. Short answer: Global variables with no specific initializer are indeed zeroed. Those with initializers take the specified initial value. Automatic variables are not initialized and could have any value.

Lee

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

For the mega32, '__do_clear_bss' from libgcc.S -
http://gcc.gnu.org/cgi-bin/cvswe...
(clears sram in the .bss section, if any)

'__do_copy_data' will setup your initialized data (copy data from flash to sram for initialized data,if any)

look at your map file and you will find these functions are coming from the library 'libgcc.a' (a compiled libgcc.S specific for the avr 'type')

with GCC, any sram not in the .data or .bss section, will be untouched

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

Quote:
Yep, all ram gets cleared..usually..

Just to be pedantic but in GCC it's ONLY the .bss tghat gets cleared. The .data gets the initialisers copied over and the rest remains untouched - which is why .noinit is possible, cos otherwise it'd be wiped too.

In CV, on the other hand, so that it doesn't need to know and speficially wipe between the bounds of .bss it DOES actually wipe the whole of RAM and then copy the selected .data initialisers.

Cliff

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

theusch wrote:
Global variables with no specific initializer are indeed zeroed. Those with initializers take the specified initial value. Automatic variables are not initialized and could have any value.

Lee


I remember one sentence from K&R which I paraphrase:

"External variables which are not initialized are guaranteed to start off as 0; automatics which are not initialized are guaranteed to start off as garbage."

The other thing I remember about K&R is that the first chapter is chapter 0. :)

Even though K&R "guarantees" it, there are no guarantees in this life. I wouldn't be surprised if there are some quick and dirty C compilers, linkers, loaders, for some small quaint devices that don't zero out the externs.

Last Edited: Sun. Apr 20, 2008 - 03:29 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I had never noticed Chapter 0 !!!

I can point to several C compilers that do or did not handle the .bss correctly.

But then there are lots of things in the world that do not always obey the rules.

David.

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

david.prentice wrote:
I had never noticed Chapter 0 !!!

I'll bet most people didn't notice. It took me ten years to see that.

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

Quote:
It took me ten years to see that.

It would take me even longer since the version I have starts with chapter 1.

Regards,
Steve A.

The Board helps those that help themselves.

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

The original version does indeed start with Chapter 0 (the introduction), but the Second edition - based on draft ANSI does not have a chapter number for the introduction. Just out of interest, is there a later K&R, based on the final ANSI C specification?

Four legs good, two legs bad, three legs stable.

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

Quote:
The original version does indeed start with Chapter 0
..and after chapter 9 comes chapter 0x0a...only natural. :?

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly