This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Unwanted optimize

My program works wrong because of c compiler's optimization. If I use "volatile" to declare some varibles and functions, the optimization will be affetced, and perhaps the program works well, but if I changed the program, I would have to add "volatile" to other varibles or functions. I have to try many times to make compiler compile my program "correctly" after change the program.

Is there a way to remove optimization completely? I use
#pragma SAVE
#pragma OPTIMIZE(0)

my_func1()
{
...;
}

#pragma RESTORE

but I know optimize level 0 doesn't mean no optimization, and my program can't be compiled correctly even at optimize level 0, if without "volatile" to most varibles and functions.

I only use P0, P1 and P2, to read and write some IO lines. Compiler is keil c51 v6.12

Parents
  • Could you show us an example of the incorrect optimization with C source and assembly code output from the compiler?

    I don't understand what you mean by a volatile function. You can't declare a function "volatile" in C.

    "volatile" is supposed to affect the optimization of code. That's its main purpose, actually.

    If, without "volatile", your program doesn't work correctly, it could mean that you have problems with updating shared variables and getting interrupted inside critical sections.

Reply
  • Could you show us an example of the incorrect optimization with C source and assembly code output from the compiler?

    I don't understand what you mean by a volatile function. You can't declare a function "volatile" in C.

    "volatile" is supposed to affect the optimization of code. That's its main purpose, actually.

    If, without "volatile", your program doesn't work correctly, it could mean that you have problems with updating shared variables and getting interrupted inside critical sections.

Children