AS7 local variables inside main() not working

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

Good morning,

 

the picture come from è very simple project to demostrate the problem.

AS7    ATmega328P      Pack ATmega_DPF  1.4.351 version.

Some suggestions will be greatly appreciated

 

That's works.That doesn't works.

This topic has a solution.
Last Edited: Wed. Jun 24, 2020 - 02:22 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That warning seems perfectly clear and self-explanatory - and obviously true!

 

alessiocarlesi wrote:
the problem

What problem ?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Tanks You awneil.

It's obvious  for you. The problem is that the build stop building.

For some reason now it works.

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

alessiocarlesi wrote:
It's obvious  for you.

Is it not obvious to you, too?

 

The message says that you assign a value to 'i', but you never use that value - which is entirely true and accurate.

 

What is unclear about that ?

 

The problem is that the build stop building

and how were we supposed to know that?

 there is nothing in your post to show that - a warning should not stop a build - and you did not mention it.

 

Note: rather than using the 'Error List' - which can be more confusing that helpful - you will get more & better information by looking at the 'Output' window:

 

https://www.avrfreaks.net/commen...

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Wed. Jun 24, 2020 - 11:00 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

Oh, wait a minute:

 

alessiocarlesi wrote:
the build stop building.

 

You do realise that the 'Build' button will only actually do any building when there is something new to build ?

 

ie, after you have made some changes.

 

So if you pressed 'Build', saw the warning, then just pressed 'Build' again - it would be expected not to do a build.

 

To make it build you would have to either:

 

  1. Make some changes, or
  2. Use 'Rebuild' instead.

 

EDIT

 

This would be the same on any IDE - it's not specific to Atmel Studio.

 

Note that you can force the compiler to treat Warnings as Errors - and thus fail the build - in the Project Properties:

 

 

https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Wed. Jun 24, 2020 - 11:11 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In that 

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

Thank for help.

Maybe there is something  wrong in my AS7 enviroment.

In short I can't use local variable inside main() for recursive loop or some other.

For the moment i'm go ahed only declaring variable outside main.

Thank You again. 

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

If a variable is assigned a value, but then never used, the compiler will (generallY) optimize it away as being unnecessary.

David (aka frog_jr)

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

alessiocarlesi wrote:
I can't use local variable inside main() for recursive loop or some other.

But the code you posted does not demonstrate that - apart from assigning a literal constant value, you never use the variables at all!

 

Which is exactly what the warning message told you.

 

Please post an example which actually demonstrates the problem.

 

And, as  frog_jr mentioned, remember that the compiler will optimise-away code that does nothing - including variables which are not used.

 

The optimiser may also choose to just have the value in a register - so you won't be able to see it in memory.

 

Again, this is all standard stuff - not specific to Atmel Studio or GCC.

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

frog_jr wrote:
, but then never used, the compiler will (generallY) optimize it away as being unnecessary.
May help to read this tutorial:

 

https://www.avrfreaks.net/forum/...

 

GCC is very aggressive at discarding code. Making things "volatile" says "this may look pointless but I command you to do it anyway"

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you very much everyone.
I'm coming from assembler enviroment so I think the tutorial will be  useful .

Meanwhile i saw other "strange" things in variable declaration and visibility that can leave in a trap.

Is not easy.

 

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

alessiocarlesi wrote:
Meanwhile i saw other "strange" things in variable declaration and visibility that can leave in a trap.
I do hope most of the "odd effects" you may see (another one is the yellow arrow jumping about in non sequential order) are explained by my tutorial. You have to accept the fact if you use GCC (and properly - by which I mean not with -O0) that it is an aggressively optimising compiler that will reorder, split, discard code sections to make the geenrated code as efficient as it can while retaining the (valid!) activity you originally asked for.

 

There are advanced concepts in C like "sequence points" that can be used to take more control over the sequencing of the code but there may be a cost incurred.

 

On the whole if you want a C compiler code to generating something close (but never quite as good frown) as a skilled Asm programmer may have come up with it's best to just let it get on with its job.

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

I totally agree with you. Because I am translating complex asm code from PIC 16F887 to Arduino UNO platform and peripherals; I'm starting from the basics of the C language without taking anything for granted. For this reason it is quite easy for me to follow your step by step post which compares the C and ASM. I thought to disable the optimization during the learning process, in order not to increase the confusion that I already have in my head on my own :) Do you agree?

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

alessiocarlesi wrote:
I thought to disable the optimization during the learning process, in order not to increase the confusion that I already have in my head on my own :) Do you agree?
No because the non-optimized code is not like anything you'd really want the compiler to produce. It gives the compiler developers a chance to see what would have happened if multiple optimization steps were not applied but no one would really accept the code it generates in a "real" application. I would learn to read, follow and debug optimized code - that may sometimes mean using volatile to temporarily disable optimization on just one object simply to make viewing it easier during debugging.

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

OK. I will follow your advice.

Finally I find out the point in your post: