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

something about preprocessor

when i use preprocessor
#if 0
...
#endif

to cross out a bunch of sentences, then after building the target.
I noticed the "data" memory usage increased, why?
There is no error in the result.

Parents
  • The compiler is able to overlay data used by different functions based on analysis of the call tree. If the compiler can prove that two functions are in different paths down the call tree, then it can reuse that memory for both functions.

    If you comment out a call to a function, you remove it from the call tree. If the function continues to exist in the code, despite not being called, then the compiler has no choice but to assume the function is the root of a new call tree (as though it were an interrupt handler). Since this is a new tree, its memory cannot be shared with any other function.

    If the function had overlaid memory, after you comment out the call it will no longer share that memory, and total usage will increase.

    One workaround is to #if out the body of the function as well as its call. Another is to come up with a coding trick that will keep the function in the call tree, yet not actually call it, e.g. "if (0) MyFunc();" Another is to put every single function in a different .c file, so you can modify the project file not to link it in.

    One of my top wishes for the Keil toolset is for them to implement dead code elimination in the linker so that it can find these uncalled functions, remove the code, and, importantly, not allocate the memory for the dead functions in the overlay analysis.

Reply
  • The compiler is able to overlay data used by different functions based on analysis of the call tree. If the compiler can prove that two functions are in different paths down the call tree, then it can reuse that memory for both functions.

    If you comment out a call to a function, you remove it from the call tree. If the function continues to exist in the code, despite not being called, then the compiler has no choice but to assume the function is the root of a new call tree (as though it were an interrupt handler). Since this is a new tree, its memory cannot be shared with any other function.

    If the function had overlaid memory, after you comment out the call it will no longer share that memory, and total usage will increase.

    One workaround is to #if out the body of the function as well as its call. Another is to come up with a coding trick that will keep the function in the call tree, yet not actually call it, e.g. "if (0) MyFunc();" Another is to put every single function in a different .c file, so you can modify the project file not to link it in.

    One of my top wishes for the Keil toolset is for them to implement dead code elimination in the linker so that it can find these uncalled functions, remove the code, and, importantly, not allocate the memory for the dead functions in the overlay analysis.

Children