Thread: class layout guarantees

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    88
    Quote Originally Posted by matsp View Post
    I'm pretty sure that the compiler will always put data in the order you define it. There is, however, no guarantee that the data won't have gaps in it - for example:
    Code:
    struct s {
      int a;
      char b;
      int c;
    };
    The gap between b and c is probably going to be 3 bytes.
    I would have thought just having one type (ie. u_char) would make padding a non-issue, but I'm not sure.

    Your constructor has no say in the matter of which order the data comes.
    No, but my question was more along the lines of: does having anything but a default-constructor make the class a non-POD type? I've remember read somewhere that it does. If it becomes a non-POD type, does that mean the layout isn't guaranteed?

    Of coruse, if you have virtual function(s) in your class, then there will be a vtable entry somewhere along with your data -usually the first 4 or 8 bytes.

    --
    Mats
    Oh yeah, I suppose where that comes is just random too...

    This is starting to look like a dead end.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by drrngrvy View Post
    I would have thought just having one type (ie. u_char) would make padding a non-issue, but I'm not sure.



    No, but my question was more along the lines of: does having anything but a default-constructor make the class a non-POD type? I've remember read somewhere that it does. If it becomes a non-POD type, does that mean the layout isn't guaranteed?
    I _THINK_ it only means that the compiler may insert "stuff" before or after your data that is unknown to you [such as a Vtable pointer and similar things]. But the order of the DATA that you have defined should still be in the order you "expect" it to be.
    [quote]


    What problem are you actually trying to solve?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    88
    Quote Originally Posted by matsp View Post
    What problem are you actually trying to solve?
    To exand what I said in the first post:
    Code:
    Header hdr;
    read( reinterpret_cast<u_char*>(hdr) );
    // Everything in it's right place now
    // use hdr here
    That means that the reinterpret_cast<u_char*>(hdr) would have to yield an array of unsigned chars to the read function, where the layout of the struct matches that of the expected array. Eg.
    Code:
    struct Header { u_char a; u_char b; } hdr;
    // read, as above, expecting an array like: u_char arr[] = { 'a', 'b' };
    hdr.a == 'a';
    hdr.b == 'b';
    From what everyone says, this is UB, so I'll have to do this another way. Gah.

    However, just FYI, this part seems to work fine using MSVC8 and gcc 4.* on linux. On the other hand, when I try and create a structure like the one I decoded, MSVC seems to not like it and alters the struct in some way (I'm only 60% sure that's what's happening, since I haven't bothered looking at the assembly).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 04:30 AM
  2. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 08:44 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM