GCC and Variable Initializations

Started by aerguin June 22, 2007
Hi, I'm having some big trouble when I initialize a variable in its
declaration. I'm using Eclipse + GCC (cygwin).

For example if I do

int x = 10;

Nothing, the space for x is alocated in ARM but not the 10 value
assigned to it. Doesn't matter if the x is local or global.

I don't know where the problem is, maybe it's some GCC stuff? Or my
linking and memory allocation is wrong?

I'm a bit new on these things and I'm using some linking files I
found on the internet by James Lynch, it looks like he knows what he
does by the tutorials he's written. Anyways, supposedly I'm writing
my program into Flash and on startup I copy the initialized variables
from Flash to RAM for use in the program.

At first glance it seems that the problem could be in this copying.
BUT, if I do this:

main(){
int a, b;
int x = 10;

bla bla // other things nothing to do with x

x = 20;
a = x+5;
x = 30;
b = x*2;
}

Here, when debugging I see that right from the beginning of the main
function the x stored in RAM has a value of 20! (I mean, before the
program reaches the "x " line). It completely ignores the 10 in
declaration. Later on it'll change to 30 in its corresponding place.
If I delete the line "a=x+5", then the value in RAM is 30 from the
beginning, ignoring the "x ". If "x " or "x0" weren't there,
the value of x would just be rubbish.
So the value copied into RAM is always the last one (excluding
declaration) before where x is used. I find this strange.

Is this my bad or a GCC feature?
Thanks!

An Engineer's Guide to the LPC2100 Series

If you compile with -O0 it should do everything like your program says.
If you use any optimization it will ignore everything that isn't strictly
needed.
Now if you set x as volatile, every write/read of it will be done regardless
of optimization.

volatile int x = 10;
x = 20;

should result in a value of 10 being written and then 20 following that.

Baldur

On Fri, Jun 22, 2007 at 02:30:39PM -0000, aerguin wrote:
> Hi, I'm having some big trouble when I initialize a variable in its
> declaration. I'm using Eclipse + GCC (cygwin).
>
> For example if I do
>
> int x = 10;
>
> Nothing, the space for x is alocated in ARM but not the 10 value
> assigned to it. Doesn't matter if the x is local or global.
>
> I don't know where the problem is, maybe it's some GCC stuff? Or my
> linking and memory allocation is wrong?
>
> I'm a bit new on these things and I'm using some linking files I
> found on the internet by James Lynch, it looks like he knows what he
> does by the tutorials he's written. Anyways, supposedly I'm writing
> my program into Flash and on startup I copy the initialized variables
> from Flash to RAM for use in the program.
>
> At first glance it seems that the problem could be in this copying.
> BUT, if I do this:
>
> main(){
> int a, b;
> int x = 10;
>
> bla bla // other things nothing to do with x
>
> x = 20;
> a = x+5;
> x = 30;
> b = x*2;
> }
>
> Here, when debugging I see that right from the beginning of the main
> function the x stored in RAM has a value of 20! (I mean, before the
> program reaches the "x " line). It completely ignores the 10 in
> declaration. Later on it'll change to 30 in its corresponding place.
> If I delete the line "a=x+5", then the value in RAM is 30 from the
> beginning, ignoring the "x ". If "x " or "x0" weren't there,
> the value of x would just be rubbish.
> So the value copied into RAM is always the last one (excluding
> declaration) before where x is used. I find this strange.
>
> Is this my bad or a GCC feature?
> Thanks!
>
--- In l..., "aerguin" wrote:
>
> Hi, I'm having some big trouble when I initialize a variable in its
> declaration. I'm using Eclipse + GCC (cygwin).
>
> For example if I do
>
> int x = 10;
>
> Nothing, the space for x is alocated in ARM but not the 10 value
> assigned to it. Doesn't matter if the x is local or global.
>
> I don't know where the problem is, maybe it's some GCC stuff? Or my
> linking and memory allocation is wrong?
>
> I'm a bit new on these things and I'm using some linking files I
> found on the internet by James Lynch, it looks like he knows what he
> does by the tutorials he's written. Anyways, supposedly I'm writing
> my program into Flash and on startup I copy the initialized variables
> from Flash to RAM for use in the program.
>
> At first glance it seems that the problem could be in this copying.
> BUT, if I do this:
>
> main(){
> int a, b;
> int x = 10;
>
> bla bla // other things nothing to do with x
>
> x = 20;
> a = x+5;
> x = 30;
> b = x*2;
> }
>
> Here, when debugging I see that right from the beginning of the main
> function the x stored in RAM has a value of 20! (I mean, before the
> program reaches the "x " line). It completely ignores the 10 in
> declaration. Later on it'll change to 30 in its corresponding place.
> If I delete the line "a=x+5", then the value in RAM is 30 from the
> beginning, ignoring the "x ". If "x " or "x0" weren't there,
> the value of x would just be rubbish.
> So the value copied into RAM is always the last one (excluding
> declaration) before where x is used. I find this strange.
>
> Is this my bad or a GCC feature?
> Thanks!
>

No, it's a compiler optimization thing. Just because you write it, it
doesn't mean they'll execute it.

If the compiler knows with certainty that some code is meaningless, it
will remove it.

You didn't say what optimization level you were using as an option for
arm-elf-gcc. You can try -O0 and see if the code is different. Or,
you can eliminate the -O option and the default will be -O0.

Richard
There could well be a simple explanation for this. Do you have any form of
optimisation on? If you do the compiler may well correctly note that you
never use x with a value of 10 before you reassign a new value of 20 to
it... so it can simply optimize the process and start it at 20.

ie, why should it waste cycles putting a value into a register for you when
it knows that you aren't going to use that value and will shortly replace it
with a different one?

Try putting your line: a = x + 5;

directly after your declaration of 'x' and see what 'a' gets set to. I would
imagine if you do this it will be 15 and x will now be 10 as you expect.
Primarily because you have used x with the value of 10 before changing it's
value to 20.
-----Original Message-----
From: l... [mailto:l...]On Behalf Of
aerguin
Sent: 22 June 2007 15:31
To: l...
Subject: [lpc2000] GCC and Variable Initializations
Hi, I'm having some big trouble when I initialize a variable in its
declaration. I'm using Eclipse + GCC (cygwin).

For example if I do

int x = 10;

Nothing, the space for x is alocated in ARM but not the 10 value
assigned to it. Doesn't matter if the x is local or global.

I don't know where the problem is, maybe it's some GCC stuff? Or my
linking and memory allocation is wrong?

I'm a bit new on these things and I'm using some linking files I
found on the internet by James Lynch, it looks like he knows what he
does by the tutorials he's written. Anyways, supposedly I'm writing
my program into Flash and on startup I copy the initialized variables
from Flash to RAM for use in the program.

At first glance it seems that the problem could be in this copying.
BUT, if I do this:

main(){
int a, b;
int x = 10;

bla bla // other things nothing to do with x

x = 20;
a = x+5;
x = 30;
b = x*2;
}

Here, when debugging I see that right from the beginning of the main
function the x stored in RAM has a value of 20! (I mean, before the
program reaches the "x " line). It completely ignores the 10 in
declaration. Later on it'll change to 30 in its corresponding place.
If I delete the line "a=x+5", then the value in RAM is 30 from the
beginning, ignoring the "x ". If "x " or "x0" weren't there,
the value of x would just be rubbish.
So the value copied into RAM is always the last one (excluding
declaration) before where x is used. I find this strange.

Is this my bad or a GCC feature?
Thanks!
> main(){
> int a, b;
> int x = 10;
>
> bla bla // other things nothing to do with x
>
> x = 20;
> a = x+5;
> x = 30;
> b = x*2;
> }

If I delete the line "a=x+5", then the value in RAM is 30 from the
> beginning, ignoring the "x ". If "x " or "x0" weren't there,
> the value of x would just be rubbish.
> So the value copied into RAM is always the last one (excluding
> declaration) before where x is used. I find this strange.
>
> Is this my bad or a GCC feature?
> Thanks!

That's the optimizer working hard so you don't have too. Think
of it as a lazy teenager :-)

You said x is 10, never used it, then told me it was 20 and never
used it and NOW you're telling me it's 30?

Why don't I just set it to 30 and save us both a lot of grief?

Oh, it's not what you thought you meant?

Parents just don't understand....

This is meant to be funny, not a flame :-)

Cheers, Ralph
If you declare your variable "volatile" it will set it to 10, then 20.

main() {
volatile int x = 10;

Or, if you declare it "static" it should also do the same.

main() {
static int x = 10;
If you put the variable outside of the function, it should also initialize
it to 10 before main() starts running.

int x = 10;
main() {
Ed Koffeman

> -----Original Message-----
> From: l... [mailto:l...] On Behalf
> Of aerguin
> Sent: Friday, June 22, 2007 9:31 AM
> To: l...
> Subject: [lpc2000] GCC and Variable Initializations
>
> Hi, I'm having some big trouble when I initialize a variable in its
> declaration. I'm using Eclipse + GCC (cygwin).
>
> For example if I do
>
> int x = 10;
>
> Nothing, the space for x is alocated in ARM but not the 10 value
> assigned to it. Doesn't matter if the x is local or global.
>
> I don't know where the problem is, maybe it's some GCC stuff? Or my
> linking and memory allocation is wrong?
>
> I'm a bit new on these things and I'm using some linking files I
> found on the internet by James Lynch, it looks like he knows what he
> does by the tutorials he's written. Anyways, supposedly I'm writing
> my program into Flash and on startup I copy the initialized variables
> from Flash to RAM for use in the program.
>
> At first glance it seems that the problem could be in this copying.
> BUT, if I do this:
>
> main(){
> int a, b;
> int x = 10;
>
> bla bla // other things nothing to do with x
>
> x = 20;
> a = x+5;
> x = 30;
> b = x*2;
> }
>
> Here, when debugging I see that right from the beginning of the main
> function the x stored in RAM has a value of 20! (I mean, before the
> program reaches the "x " line). It completely ignores the 10 in
> declaration. Later on it'll change to 30 in its corresponding place.
> If I delete the line "a=x+5", then the value in RAM is 30 from the
> beginning, ignoring the "x ". If "x " or "x0" weren't there,
> the value of x would just be rubbish.
> So the value copied into RAM is always the last one (excluding
> declaration) before where x is used. I find this strange.
>
> Is this my bad or a GCC feature?
> Thanks!
> Yahoo! Groups Links
>
Mmmmm you all point to the optimization thing, but unfortunatelly I
don't think that is the reason :(
I am using -O0 in all my sessions.

Also, probably I didn't explain the most important case, the one
that bothers me:

main(){

int a, b;
int x = 10;

a = x + 5;
x = 15;
b = x + 5;
}

After this, the value in 'a' will be 20! (yes, 15 + 5). The value
in 'b' will also be 20, no problem here.

The problem, simply, is that if I initialize a value in its
DECLARATION, the variable never takes that value. If, in the above
case, I hadn't put "x" later on, both the values in 'a' and 'b'
would be non-initialized rubbish.
So the value that goes into RAM is the one that will be actively
used, but NOT if this value was given in the declaration.

Another point to mention in the above case, which probably clears
some things out for you (not for me, it only confuses me more), is
the values of the x variable in Flash and RAM in the beginning of
the main function.
The value in Flash is 10. Perfect!! It seems that the compiler
didn't optimize anything, and that it did take into account the
initialization. But the value in RAM is 15, BEFORE the program
reached that line. !!?? :(
Supposedly I copied Flash variables into RAM at startup, so why a
different value?
--- In l..., "Andy Berney" wrote:
>
> There could well be a simple explanation for this. Do you have any
form of
> optimisation on? If you do the compiler may well correctly note
that you
> never use x with a value of 10 before you reassign a new value of
20 to
> it... so it can simply optimize the process and start it at 20.
>
> ie, why should it waste cycles putting a value into a register for
you when
> it knows that you aren't going to use that value and will shortly
replace it
> with a different one?
>
> Try putting your line: a = x + 5;
>
> directly after your declaration of 'x' and see what 'a' gets set
to. I would
> imagine if you do this it will be 15 and x will now be 10 as you
expect.
> Primarily because you have used x with the value of 10 before
changing it's
> value to 20.
> -----Original Message-----
> From: l... [mailto:l...]On
Behalf Of
> aerguin
> Sent: 22 June 2007 15:31
> To: l...
> Subject: [lpc2000] GCC and Variable Initializations
> Hi, I'm having some big trouble when I initialize a variable in
its
> declaration. I'm using Eclipse + GCC (cygwin).
>
> For example if I do
>
> int x = 10;
>
> Nothing, the space for x is alocated in ARM but not the 10 value
> assigned to it. Doesn't matter if the x is local or global.
>
> I don't know where the problem is, maybe it's some GCC stuff? Or
my
> linking and memory allocation is wrong?
>
> I'm a bit new on these things and I'm using some linking files I
> found on the internet by James Lynch, it looks like he knows
what he
> does by the tutorials he's written. Anyways, supposedly I'm
writing
> my program into Flash and on startup I copy the initialized
variables
> from Flash to RAM for use in the program.
>
> At first glance it seems that the problem could be in this
copying.
> BUT, if I do this:
>
> main(){
> int a, b;
> int x = 10;
>
> bla bla // other things nothing to do with x
>
> x = 20;
> a = x+5;
> x = 30;
> b = x*2;
> }
>
> Here, when debugging I see that right from the beginning of the
main
> function the x stored in RAM has a value of 20! (I mean, before
the
> program reaches the "x " line). It completely ignores the 10 in
> declaration. Later on it'll change to 30 in its corresponding
place.
> If I delete the line "a=x+5", then the value in RAM is 30 from
the
> beginning, ignoring the "x ". If "x " or "x0" weren't
there,
> the value of x would just be rubbish.
> So the value copied into RAM is always the last one (excluding
> declaration) before where x is used. I find this strange.
>
> Is this my bad or a GCC feature?
> Thanks!
>
>
>
>
Why not simply step through the code with the debugger set to assembly
language and find out what is actually going on?

--Dave
--- In l..., "aerguin" wrote:
>
> Mmmmm you all point to the optimization thing, but unfortunatelly I
> don't think that is the reason :(
> I am using -O0 in all my sessions.
>
> Also, probably I didn't explain the most important case, the one
> that bothers me:
>
> main(){
>
> int a, b;
> int x = 10;
>
> a = x + 5;
> x = 15;
> b = x + 5;
> }
>
> After this, the value in 'a' will be 20! (yes, 15 + 5). The value
> in 'b' will also be 20, no problem here.
>
> The problem, simply, is that if I initialize a value in its
> DECLARATION, the variable never takes that value. If, in the above
> case, I hadn't put "x" later on, both the values in 'a' and 'b'
> would be non-initialized rubbish.
> So the value that goes into RAM is the one that will be actively
> used, but NOT if this value was given in the declaration.
>
> Another point to mention in the above case, which probably clears
> some things out for you (not for me, it only confuses me more), is
> the values of the x variable in Flash and RAM in the beginning of
> the main function.
> The value in Flash is 10. Perfect!! It seems that the compiler
> didn't optimize anything, and that it did take into account the
> initialization. But the value in RAM is 15, BEFORE the program
> reached that line. !!?? :(
> Supposedly I copied Flash variables into RAM at startup, so why a
> different value?
aerguin Wrote
>Mmmmm you all point to the optimization thing, but unfortunatelly I
>don't think that is the reason :(
>I am using -O0 in all my sessions.

Everyone has made the assumption that -O0 will prevent that optimization.
It might, but then again...

>Also, probably I didn't explain the most important case, the one
>that bothers me:
>
>main(){
>
>int a, b;
>int x = 10;
>
>a = x + 5;
>x = 15;
>b = x + 5;
>}

There is, of course, no reason this has to generate any code other than a
return.

>After this, the value in 'a' will be 20! (yes, 15 + 5). The value
>in 'b' will also be 20, no problem here.

Since you are not using the value, it may be assigning it to a register
using that register in the calculation and then since it is done with it
reusing the same register for b.
>The problem, simply, is that if I initialize a value in its
>DECLARATION, the variable never takes that value. If, in the above
>case, I hadn't put "x" later on, both the values in 'a' and 'b'
>would be non-initialized rubbish.

Not a problem I've ever seen but that appears to be more likely a
linker/link script issue rather than a compiler issue.

>So the value that goes into RAM is the one that will be actively
>used, but NOT if this value was given in the declaration.
>
>Another point to mention in the above case, which probably clears
>some things out for you (not for me, it only confuses me more), is
>the values of the x variable in Flash and RAM in the beginning of
>the main function.
>The value in Flash is 10. Perfect!! It seems that the compiler
>didn't optimize anything, and that it did take into account the
>initialization. But the value in RAM is 15, BEFORE the program
>reached that line. !!?? :(
>Supposedly I copied Flash variables into RAM at startup, so why a
>different value?

non-static locals are not initialized that way. They will be initialized
at the start of the function.

That's a short routine, I suggest you take a look at the generated
assembly. That'll give you an idea of where it's going off the rails.

Oh, and there is no reason x couldn't be in a register as well, it might
even be replaced by a constant.

Robert
--------------------------------
mail2web.com - Microsoft Exchange solutions from a leading provider -
http://link.mail2web.com/Business/Exchange
I'm trying to debug the start-up code, but the debugger will always
go directly to the first instruction of the main function. From here
on there's not much point debugging in assembly (I've done it) as
the initializations, or the non-initializations in this case, are
already done.
Do you know how I could debug the start-up code before the main? I'm
using Eclipse + gdb + openOCD.

--- In l..., "derbaier" wrote:
> Why not simply step through the code with the debugger set to
assembly
> language and find out what is actually going on?
>
> --Dave
> --- In l..., "aerguin" wrote:
> >
> > Mmmmm you all point to the optimization thing, but
unfortunatelly I
> > don't think that is the reason :(
> > I am using -O0 in all my sessions.
> >
> > Also, probably I didn't explain the most important case, the one
> > that bothers me:
> >
> > main(){
> >
> > int a, b;
> > int x = 10;
> >
> > a = x + 5;
> > x = 15;
> > b = x + 5;
> > }
> >
> > After this, the value in 'a' will be 20! (yes, 15 + 5). The
value
> > in 'b' will also be 20, no problem here.
> >
> > The problem, simply, is that if I initialize a value in its
> > DECLARATION, the variable never takes that value. If, in the
above
> > case, I hadn't put "x" later on, both the values in 'a'
and 'b'
> > would be non-initialized rubbish.
> > So the value that goes into RAM is the one that will be actively
> > used, but NOT if this value was given in the declaration.
> >
> > Another point to mention in the above case, which probably
clears
> > some things out for you (not for me, it only confuses me more),
is
> > the values of the x variable in Flash and RAM in the beginning
of
> > the main function.
> > The value in Flash is 10. Perfect!! It seems that the compiler
> > didn't optimize anything, and that it did take into account the
> > initialization. But the value in RAM is 15, BEFORE the program
> > reached that line. !!?? :(
> > Supposedly I copied Flash variables into RAM at startup, so why
a
> > different value?
> >
>