Skip to main content
Post Reopened by n. m. could be an AI c++
explain more
Source Link

Does g++ Will gcc remove unused class field, in the final executable file?

For example, will gcc/g++(version 9.4, or 7.3) remove unused c and pc in the output file, because this piece of code access only C::i and C::j, but not C::c andC::pc.

#include <iostream> struct C { // i, j is used below int i; int j; // c, pc is not used char c; char *pc; C() : i(2) {} }; int main() { C c; c.j = 9; std::cout << c.j << c.i << sizeof(c) << std::endl; } 


The problem I am confused with is almost solved, inspired by the site.

Now I understand that the field is accessed as a pointer plus an offset, thus without an access, there will be no code representing as such a pointer plus an offset, in the final executable file.

Does g++ remove unused class field?

For example, will gcc/g++(version 9.4, or 7.3) remove unused c and pc in the output file, because this piece of code access only C::i and C::j, but not C::c andC::pc.

#include <iostream> struct C { // i, j is used below int i; int j; // c, pc is not used char c; char *pc; C() : i(2) {} }; int main() { C c; c.j = 9; std::cout << c.j << c.i << sizeof(c) << std::endl; } 

Will gcc remove unused class field, in the final executable file?

For example, will gcc/g++(version 9.4, or 7.3) remove unused c and pc in the output file, because this piece of code access only C::i and C::j, but not C::c andC::pc.

#include <iostream> struct C { // i, j is used below int i; int j; // c, pc is not used char c; char *pc; C() : i(2) {} }; int main() { C c; c.j = 9; std::cout << c.j << c.i << sizeof(c) << std::endl; } 


The problem I am confused with is almost solved, inspired by the site.

Now I understand that the field is accessed as a pointer plus an offset, thus without an access, there will be no code representing as such a pointer plus an offset, in the final executable file.

change the code example
Source Link

For example, will gcc/g++(version 9.4, or 7.3) remove unused c and pc in the output file:, because this piece of code access only C::i and C::j, but not C::c andC::pc.

#include <iostream> struct C { // i, j is used below int i; int j; // c, pc is not used char c; //char *pc; C() : i(2) {}   }; int main() { C c; c.j = 9; std::cout << c.j << c.i << sizeof(c) << std::endl; } 

For example, will gcc/g++(version 9.4, or 7.3) remove unused c and pc in the output file:

#include <iostream> struct C { int i; int j; //char c; //char *pc; C() : i(2) {}   }; int main() { C c; c.j = 9; std::cout << c.j << c.i << sizeof(c) << std::endl; } 

For example, will gcc/g++(version 9.4, or 7.3) remove unused c and pc in the output file, because this piece of code access only C::i and C::j, but not C::c andC::pc.

#include <iostream> struct C { // i, j is used below int i; int j; // c, pc is not used char c; char *pc; C() : i(2) {} }; int main() { C c; c.j = 9; std::cout << c.j << c.i << sizeof(c) << std::endl; } 
simplify the question, highlight the point
Added to review
Source Link

Will gcc/g++ Does g++ remove unused class data memberfield?

I found that it does not affect the size of the binary output whether I comment out the unused data member or notFor example, using g++ versionwill gcc/g++(version 9.4, but some say it does, using g++ versionor 7.3.

I wonder if there is some optimization done for this) remove unused c and pc in higher version of the compiler.

here is the exampleoutput file:

#include <iostream> struct C { int i; int j; //char c; //char *pc; C() : i(2) {} }; int main() { C c; c.j = 9; std::cout << c.j << c.i << sizeof(c) << std::endl; } 

Will gcc/g++ remove unused class data member?

I found that it does not affect the size of the binary output whether I comment out the unused data member or not, using g++ version 9.4, but some say it does, using g++ version 7.3.

I wonder if there is some optimization done for this in higher version of the compiler.

here is the example:

#include <iostream> struct C { int i; int j; //char c; //char *pc; C() : i(2) {} }; int main() { C c; c.j = 9; std::cout << c.j << c.i << sizeof(c) << std::endl; } 

Does g++ remove unused class field?

For example, will gcc/g++(version 9.4, or 7.3) remove unused c and pc in the output file:

#include <iostream> struct C { int i; int j; //char c; //char *pc; C() : i(2) {} }; int main() { C c; c.j = 9; std::cout << c.j << c.i << sizeof(c) << std::endl; } 
Post Closed as "Duplicate" by Richard c++
Source Link
Loading