This is perhaps a dumb question, but do compilers remove included or written code if it's never used by rest of the program?
This is perhaps a dumb question, but do compilers remove included or written code if it's never used by rest of the program?
They shouldn't, and if yours does, you might need a new compiler, but let's check anyway:
Code:#include <stdio.h> void data() { printf("I really dont do anything at all\n"); } int main() { printf("Hello World!"); return 0; }(The -S tag is simply for outputting a .S (assembler) file instead of compiling straight to binary. You could also use -save-temps but -S is what we would rather have, it's not an optimization flag.)C:\WINDOWS\Desktop>C:\mingw\bin\gcc -S test.c
C:\WINDOWS\Desktop>
MinGW without -O2
MinGW with -O2:Code:... LC0: .ascii "I really dont do anything at all\12\0" .text .globl _data .def _data; .scl 2; .type 32; .endef _data: pushl %ebp movl %esp, %ebp subl $8, %esp movl $LC0, (%esp) call _printf leave ret ...
Nope. Let's even go into passing the strip tag.Code:LC0: .ascii "I really dont do anything at all\0" .text .p2align 4,,15 .globl _data .def _data; .scl 2; .type 32; .endef _data: pushl %ebp movl %esp, %ebp subl $8, %esp movl $LC0, (%esp) call _puts leave ret
MinGW with -O3 and -s:
Nope. Doesn't remove anything. Optimizations aside, your code will bascially come out the same.Code:LC0: .ascii "I really dont do anything at all\0" .text .p2align 4,,15 .globl _data .def _data; .scl 2; .type 32; .endef _data: pushl %ebp movl %esp, %ebp subl $8, %esp movl $LC0, (%esp) call _puts leave ret
Last edited by Mad_guy; 12-26-2005 at 02:08 PM.
operating systems: mac os 10.6, debian 5.0, windows 7
editor: back to emacs because it's more awesomer!!
version control: git
website: http://0xff.ath.cx/~as/
No, they shouldn't, as state above. But you might be confusing the compiler with the linker, which might not link unused object code in the final executable. For example, if you include stdio.h but never use printf, the object code for printf doesn't necessarily have to be linked in.This is perhaps a dumb question, but do compilers remove included or written code if it's never used by rest of the program?
I know VC++ 6.0 will throw out unused code during optimization stage -- the compiler is documented to do that. So if you write a function foo() that is never called the compiler will discard it in the final exe program.
It depends how obvious the omission is, and it depends on the compiler/linker, and I'm not sure what one gains from this knowledge. The C standard doesn't care.
My compiler will include the printf call and the "Hello, world!\n" string literal if I do:
But not if I change a == 5 to a == a.Code:#include <stdio.h> int main(void) { int a = 5; if (a==5) return 0; printf("Hello, world!\n"); return 0; }
However, if I keep it as a == a, but change a's type to a double, it includes the code again, since a double does not always return true when compared to itself.
They will optimize un-optimized loops and they will remove redundant code if specified. This can change the final assembly to something you might not expect.
> but let's check anyway:
Repeat your experiment with a static function.
Attempts like
/* waste a bit of time */
for ( i = 0 ; i < 1000000 ; i++ );
Often cause surprise to newbies when they finally figure out that the compiler got rid of the loop because it didn't do anything useful.