Does an unused member variable take up memory?

前端 未结 6 1941
一生所求
一生所求 2020-12-13 03:00

Does initializing a member variable and not referencing/using it further take up RAM during runtime, or does the compiler simply ignore that variable?

struct         


        
6条回答
  •  长情又很酷
    2020-12-13 03:38

    The compiler will only optimise away an unused member variable (especially a public one) if it can prove that removing the variable has no side effects and that no part of the program depends on the size of Foo being the same.

    I don't think any current compiler performs such optimisations unless the structure isn't really being used at all. Some compilers may at least warn about unused private variables but not usually for public ones.

提交回复
热议问题