Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Message

Re: Volatile modifier

2012-04-17 by Don Kinzer

--- In AVR-Chat@yahoogroups.com, "bayramdavies" <Yahoo37849@...> wrote:
>> ... unless it can determine conclusively otherwise.
> Which, I think, it generally can't for the C language.
For most languages, C included, it is conceptually easy: look at a function and see what global variables it might modify and then do the same for any function that it might call and so on.  Different compilers may go to varying lengths to try to optimize out reloading of globals and there are situations that complicate matters (e.g. writing through a pointer having an unknown value).  

If you compile the example code below with WinAVR_20100110 using -Os you'll see that the compiler inlines the foo() code into main() and determines that the variable b1 is loop invariant.  Granted, it is a simple example but it shows that it can be and is done.

uint8_t b1;
uint8_t b2;

void foo(void)
{
    b2++;
}

int main(void)
{
    while (b1)
        foo();
    for (;;)
        ;
}


Don Kinzer
ZBasic Microcontrollers
http://www.zbasic.net

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.