Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. class align vs struct align

class align vs struct align

Scheduled Pinned Locked Moved C / C++ / MFC
visual-studiotutorialquestion
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    oobimoo
    wrote on last edited by
    #1

    Hello to everyone (as this is my first question here) I'm trying to understand how class members aligned (with msvc compiler). I was expecting that this would be identical with a struct. And it is as long as the class doesn't contain virtual methods. But when it does sometimes are equal sometimes not. For example

    struct s {
    unsigned* p;
    int n;
    double d;
    };
    //i get size 16, but for the class
    class cc {
    public:
    virtual void msg() { cout<<"c::msg"<<endl; }
    private:
    int n;
    double d;
    };
    //i get size 24

    thanks in advance

    modified on Tuesday, August 12, 2008 6:03 PM

    B J 2 Replies Last reply
    0
    • O oobimoo

      Hello to everyone (as this is my first question here) I'm trying to understand how class members aligned (with msvc compiler). I was expecting that this would be identical with a struct. And it is as long as the class doesn't contain virtual methods. But when it does sometimes are equal sometimes not. For example

      struct s {
      unsigned* p;
      int n;
      double d;
      };
      //i get size 16, but for the class
      class cc {
      public:
      virtual void msg() { cout<<"c::msg"<<endl; }
      private:
      int n;
      double d;
      };
      //i get size 24

      thanks in advance

      modified on Tuesday, August 12, 2008 6:03 PM

      B Offline
      B Offline
      Bram van Kampen
      wrote on last edited by
      #2

      That is probably because your allignment is set to 8. Try setting it to 4,and you should get sizes 8 and 12. :)

      Bram van Kampen

      O 1 Reply Last reply
      0
      • B Bram van Kampen

        That is probably because your allignment is set to 8. Try setting it to 4,and you should get sizes 8 and 12. :)

        Bram van Kampen

        O Offline
        O Offline
        oobimoo
        wrote on last edited by
        #3

        Thanks for the quick response Bram. Unfortunately this is not my problem. So let me make it more clear. I have a class with a virtual method (or two or a hudrent, there is no difference), an integer and a double and a 32bit os. So i expect to get 4 bytes for the vptr, 4 bytes for the int, 8 for the double and no need for padding anywhere between, because they are already aligned. Exactly what i have in the struct (pointer, int, double). Why do i get different sizes and member alignment?

        modified on Tuesday, August 12, 2008 6:03 PM

        B 1 Reply Last reply
        0
        • O oobimoo

          Thanks for the quick response Bram. Unfortunately this is not my problem. So let me make it more clear. I have a class with a virtual method (or two or a hudrent, there is no difference), an integer and a double and a 32bit os. So i expect to get 4 bytes for the vptr, 4 bytes for the int, 8 for the double and no need for padding anywhere between, because they are already aligned. Exactly what i have in the struct (pointer, int, double). Why do i get different sizes and member alignment?

          modified on Tuesday, August 12, 2008 6:03 PM

          B Offline
          B Offline
          Bram van Kampen
          wrote on last edited by
          #4

          Point Taken! X| Well, experimenting a bit, and checking offsetof(Struct,n) vs offsetof(Class,n), it appears that the compiler for it's own reason sets aside 8 bytes for the vtable pointer. For one thing, you can be sure that this is and will be always these 8 bytes. The Entire COM architecture relies on the proposition that a Class with a virtual function equals a vtable Pointer followed by a structure. The C interface and the CPP interface are both able to work with the same data. If this were to change, the entire COM machinery would break. :)

          Bram van Kampen

          O 1 Reply Last reply
          0
          • B Bram van Kampen

            Point Taken! X| Well, experimenting a bit, and checking offsetof(Struct,n) vs offsetof(Class,n), it appears that the compiler for it's own reason sets aside 8 bytes for the vtable pointer. For one thing, you can be sure that this is and will be always these 8 bytes. The Entire COM architecture relies on the proposition that a Class with a virtual function equals a vtable Pointer followed by a structure. The C interface and the CPP interface are both able to work with the same data. If this were to change, the entire COM machinery would break. :)

            Bram van Kampen

            O Offline
            O Offline
            oobimoo
            wrote on last edited by
            #5

            Sorry, no luck If I get rid of the double in the class, I have 8 bytes size only :(

            1 Reply Last reply
            0
            • O oobimoo

              Hello to everyone (as this is my first question here) I'm trying to understand how class members aligned (with msvc compiler). I was expecting that this would be identical with a struct. And it is as long as the class doesn't contain virtual methods. But when it does sometimes are equal sometimes not. For example

              struct s {
              unsigned* p;
              int n;
              double d;
              };
              //i get size 16, but for the class
              class cc {
              public:
              virtual void msg() { cout<<"c::msg"<<endl; }
              private:
              int n;
              double d;
              };
              //i get size 24

              thanks in advance

              modified on Tuesday, August 12, 2008 6:03 PM

              J Offline
              J Offline
              Jijo Raj
              wrote on last edited by
              #6

              Its same for class and struct. For instance, Add a virtual function to struct s and you'll get the sizeof struct s as 24.

              struct s {
              // unsigned* p;
              virtual void msg() { cout<<"c::msg"<<endl; }
              int n;
              double d;
              };

              If you disable padding and check the size of class cc, you'll get 16 bytes for your class.

              // Disable padding.
              #pragma pack(1)

              class cc {
              public:
              virtual void msg() { cout<<"c::msg"<<endl; }
              private:
              int n;
              double d;
              };

              The point is, if your class have vtable, for some reason, compiler is adding a 4 bytes padding between vtbl ptr and other member variables. If you disable padding, it will be removed. I'm not sure about the purpose. :doh: Well, I hope atleast my reply helped you to some extend. :-D Regards, Jijo.

              _____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.

              O 1 Reply Last reply
              0
              • J Jijo Raj

                Its same for class and struct. For instance, Add a virtual function to struct s and you'll get the sizeof struct s as 24.

                struct s {
                // unsigned* p;
                virtual void msg() { cout<<"c::msg"<<endl; }
                int n;
                double d;
                };

                If you disable padding and check the size of class cc, you'll get 16 bytes for your class.

                // Disable padding.
                #pragma pack(1)

                class cc {
                public:
                virtual void msg() { cout<<"c::msg"<<endl; }
                private:
                int n;
                double d;
                };

                The point is, if your class have vtable, for some reason, compiler is adding a 4 bytes padding between vtbl ptr and other member variables. If you disable padding, it will be removed. I'm not sure about the purpose. :doh: Well, I hope atleast my reply helped you to some extend. :-D Regards, Jijo.

                _____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.

                O Offline
                O Offline
                oobimoo
                wrote on last edited by
                #7

                >>if your class have vtable, for some reason, compiler is adding a 4 bytes padding between vtbl ptr and other member variables Thanks for the reply Jijo. As I mentioned before, this doesn't seems to be a rule. If I have virtual methods and just an integer for data member, there is no padding. The size is 8 bytes. I'm working on a small tutorial about inheritance, virtual calls etc in c++. Alignment is not a subject here, but since I demonstrate datamember access via offsets from the base address, I just want a clue why this happens, not the complete explanation.

                modified on Wednesday, August 13, 2008 8:11 AM

                B 1 Reply Last reply
                0
                • O oobimoo

                  >>if your class have vtable, for some reason, compiler is adding a 4 bytes padding between vtbl ptr and other member variables Thanks for the reply Jijo. As I mentioned before, this doesn't seems to be a rule. If I have virtual methods and just an integer for data member, there is no padding. The size is 8 bytes. I'm working on a small tutorial about inheritance, virtual calls etc in c++. Alignment is not a subject here, but since I demonstrate datamember access via offsets from the base address, I just want a clue why this happens, not the complete explanation.

                  modified on Wednesday, August 13, 2008 8:11 AM

                  B Offline
                  B Offline
                  Bram van Kampen
                  wrote on last edited by
                  #8

                  Hi, I've bookmarked this conversation. It merrits more investigation, particularly the effect of declaring a double. Sometimes MS has rules for their own reasons. My COM argument still stands I think. It may be a surrepticcious rule MS introduced to cover a previous bug. At least, it is unexpected behaviour. :)

                  Bram van Kampen

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups