Overhead in unused code

前端 未结 4 655
时光取名叫无心
时光取名叫无心 2021-01-02 01:29

I am wondering what the overhead is of having unused functions in your code.

Say for example you have some debug logging, and you then give most of your objects a To

4条回答
  •  心在旅途
    2021-01-02 01:46

    Linkers do remove duplicate functions and they do remove unreferenced data (the Microsoft linker offers the /OPF:REF and /OPT:ICF switches to tweak these settings).

    You are certainly right that in most cases it simply won't matter whether the linker does a good job at dropping stuff that is not needed or redundant - the impact on executable size for a few small functions (as compared to i.e. the sheer amount of code that is generated if you make extensive use of the STL or other template libraries) is minimal.

    That said, if you need your executable to be as small as possible (or if you find out that your debugging code really takes most of the image size), #ifdefing everything is the simplest way to enforce certain functions not to be included. It makes the code a bit ugly to read but it has the advantage that you can't accidentally miss few spots of debugging code in your release builds since any attempt to invoke a non-existent function will result in a compiler error.

    Another advantage of #ifdef is that it is portable and does not depend on a particular compiler system :-/

提交回复
热议问题