Vlada Katlinskaya
Vlada Katlinskaya

Reputation: 1033

GCC compiles EEPROM addresses in reverse order

I have a code for ATmega written in C and compiled with GCC compiler.

Some data need to be stored in EEPROM so I add these declarations:

enter image description here

After I read EEPROM I found that data placed in some weird way. After some investigation I found this text in .map file (one of many produced by the tool chain):

enter image description here

As you can see compiler put data in reverse order.

Of course I could reverse declaration and enjoy further coding but this is something unexpected so I'm afraid to face with any other unexpected behaviour until i don't understand why compiler did so.

Any thoughts?

Upvotes: 3

Views: 570

Answers (1)

cadaniluk
cadaniluk

Reputation: 15229

As @Lundin stated, global variables aren't required to be allocated consecutively.

You could use the feature of struct that its members always are allocated in the same order they are specified.
From the C11 standard, §6.7.2.1.15:

Within a structure object, the non-bit-field members and the units in which bit-fields reside have addresses that increase in the order in which they are declared.

Be aware of padding, though!

Upvotes: 2

Related Questions