Hi! On Thu, Mar 07, 2019 at 12:46:30PM +0100, Freddie Chopin wrote: > If I compile this file with GCC 8, with no optimizations and dump the > object file I get the variables in the exact order as in the source > file: [ ... ] > However if I enable the optimization (any level), the variables are > reversed in memory: [ ... ] > As I need to keep consistent addresses of various variables in specific > memory section for different versions of the program, I decided to put > all of these variables in the same source file. This is for an embedded > project and the section of memory I'm using has backup battery power, > so its contents are not lost when the device is powered down. However I > also need the addresses of "old" objects to remain constant and I don't > want them to change when I add some "new" objects. You probably should use a linker script for this. > So I'm wondering whether this particular behavior of GCC is something > which I could control with any compiler flag (other than disabling > optimizations [; ) and whether this is something consistent across > different versions of GCC? For example can I assume that in GCC 12, in > year 2022, the order of the variables in the memory will still be > reverse of the order in source file, or maybe someday the order will be > different than today with GCC 8? The compiler can optimise away (some of) those variables even, or not always keep them in memory, etc., unless you use volatile on the vars. Segher