Thread: Struct field containing size of struct

  1. #1
    Registered User
    Join Date
    Jun 2008
    Location
    Somewhere in Europe
    Posts
    99

    Struct field containing size of struct

    Hello,

    Why might a struct need a field containing the size of the struct (for example, in Windows programming, the first field of the struct WNDCLASSEX is generally set to sizeof(WNDCLASSEX))? Can the size of a struct not usually be found in a symbol table or the like?

    Thanks for any help.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It means microsoft can keep adding new members to the end of the structure, which new code will use (and write a bigger size).

    Old code does NOT have to be recompiled, but it will still work since the MS code will read the old (small) size and thus overlay the appropriate old structure.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Location
    Somewhere in Europe
    Posts
    99
    I see. Does this mean the new code will be compiled with a new definition of the structure (containing the additional members), which is different from the definition used for compilation of the old code?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It could just be the new one, with some flags to prevent access to non-existent members, or both structs, with one renamed to indicate a previous version.

    The point is, the old and new structs overlap exactly, so in effect you have
    Code:
    union {
      struct old {
        int foo;
      };
      struct new {
        int foo;
        int bar;
      };
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jun 2008
    Location
    Somewhere in Europe
    Posts
    99
    So you can have two structure declarations (or class definitions) with the same name in different files, with one having additional members? My compiler doesn't complain about this for C or C++; I would have thought it would contravene the one-definition rule in C++ at least.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    That would be one way of doing it, yes.

    The one definition rule is per translation unit. Two source files could each have their own "struct foo" without conflicting with the other.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Jun 2008
    Location
    Somewhere in Europe
    Posts
    99
    I had two files with following:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct point {int x; int y; int z;};
    extern struct point p2;
    
    int main()
    {
    
        struct point p1 = {0, 0, 0};
    
        p1.x = p2.x;
        printf("%d", p1.x);
        return 0;
    
    }
    and:
    Code:
    struct point {int x; int y;};
    struct point p2 = {99, 99};
    So I would have thought translation units and files would have been equivalent in this case, i.e. two translation units with conflicting definitions. I had pretty much exactly the same thing in C++ too. Compilation and linking worked OK in both cases.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    But the compiler can only check the current source file for internal consistency.

    By the time you get to the linker, all type information has gone. All the linker sees is p2 with 8 bytes of initialised data.

    If your main tried to access p2.z, it would get garbage.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How i use a struct field on a function?
    By Rafa T. in forum C Programming
    Replies: 6
    Last Post: 12-05-2010, 10:32 AM
  2. Replies: 2
    Last Post: 06-07-2009, 08:49 AM
  3. Replies: 1
    Last Post: 05-31-2009, 04:02 PM
  4. Replies: 2
    Last Post: 01-17-2009, 10:48 PM
  5. what is the size of following struct
    By vinayks in forum C Programming
    Replies: 5
    Last Post: 09-15-2005, 08:47 PM