0

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.

Aaron Q
  • 13
  • 2
  • Read about [padding](https://stackoverflow.com/questions/65026120/why-adjacent-members-of-the-same-type-can-have-padding-in-between). Also see dupe: [How to get rid of padding bytes between data members of a struct](https://stackoverflow.com/questions/3277842/how-to-get-rid-of-padding-bytes-between-data-members-of-a-struct) – Jason May 31 '23 at 09:22
  • [The size of a structure is a multiple of the alignment requirement of a data member with the largest alignment requirement.](https://stackoverflow.com/a/24242042/12002570) – Jason May 31 '23 at 09:24
  • What would it mean to "remove unused class fields"? How would you know whether gcc "removed" them? At least the value of `sizeof(c)` must be computed as if the members are present. Do the fields now count as used or as unused in your book? – j6t May 31 '23 at 10:29
  • in the code you posted `c` and `pc` are not "unused". Its just comments. Comments are only relevant for human readers but not for anything else – 463035818_is_not_an_ai May 31 '23 at 12:23
  • The compiler will ignore comments. The comments will not generate code, and will not show up in the `sizeof c` operator. – Eljay May 31 '23 at 16:21
  • 1
    Not sure this is really a duplicate or even about padding. Are you saying that the `sizeof(c)` gives the same output with or without the `char *pc;` line commented-out? – Adrian Mole Jun 01 '23 at 05:57
  • @463035818_is_not_a_number You are right, and I just re-posted the code. – Aaron Q Jun 05 '23 at 01:35
  • @AdrianMole The problem I am confused with actually is almost solved, inspired by the this: https://stackoverflow.com/questions/118068/why-doesnt-gcc-optimize-structs?rq=4. What I am concerned with is the size of the executable file, and will the fields cause an increasing of the size. – Aaron Q Jun 05 '23 at 01:58
  • This question is not related to padding. – n. m. could be an AI Jun 05 '23 at 03:53

1 Answers1

1

Compilers will not optimize out unused struct fields. In theory they might be allowed (but not required) to do so, but in practice it never happens. One reason is that the compiler normally sees only one translation unit at a time, and can never be sure that the field is not accessed in other translation units.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243