A funny code snippet of "sizeof()"

This is a discussion on A funny code snippet of "sizeof()" within the C++ Programming forums, part of the General Programming Boards category; Here is the code Code: struct A1{ char b; struct { char c; char a; }; }; cout<<sizeof(A1)<<endl; struct A2{ ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    A funny code snippet of "sizeof()"

    Here is the code

    Code:
    struct A1{
    	char b;
    	struct {
    		char c;
    		char a;
    	};
    };
    
    	cout<<sizeof(A1)<<endl;
    
    struct A2{
    	char b;
    	struct B{
    		char c;
    		char a;
    	};
    };
    	cout<<sizeof(A2)<<endl;
    It's interesting to see the size of A1 and A2 are different just because you give A2.B a name! Why is THAT?

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    You didn't actually declare struct B as a member of A2, so it's members are inaccessible and are probably eliminated by an optimizer. This would produce a smaller struct.

    On the other hand, the anonymous struct in A1 is meaningless, so it has no impact on the accessability of its members. The optimizer probably scraps the anonymous wrapper.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    The output is
    Code:
    3
    1
    I don't understand why a byte alignment is applied to A1?

    Quote Originally Posted by citizen View Post
    You didn't actually declare struct B as a member of A2, so it's members are inaccessible and are probably eliminated by an optimizer. This would produce a smaller struct.

    On the other hand, the anonymous struct in A1 is meaningless, so it has no impact on the accessability of its members. The optimizer probably scraps the anonymous wrapper.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    It's not an alignment issue. struct B is not a member of A2. Therefore, its members are not a part of the object, producing something of a smaller size.
    Code:
    #include <iostream>
    
    struct foo
    {
        char stub[3];
    };
    
    struct bar
    {
        char stub[2];
    };
    
    struct quz
    {
        char c;
        bar b;
    };
    
    int main ( )
    {
        std::cout<<"sizeof (quz) = "<<sizeof (quz)<<"\n";
        std::cout<<"sizeof (foo) = "<<sizeof (foo)<<"\n";
    }
    If you run that, foo and quz might be the same size, but this is only because quz actually has a bar.
    Last edited by whiteflags; 05-18-2008 at 09:18 PM.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Code:
    ~/formus $ g++ -o anonstruct -ansi -pedantic -Wall anonstruct.cpp
    anonstruct.cpp:9: error: ISO C++ prohibits anonymous structs
    Simply put, you've hit a compiler extension, so you shouldn't be surprised that it does weird things.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 11:38 AM
  2. snippet of for loop code problem
    By rayrayj52 in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2004, 02:36 PM
  3. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 03:28 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. pigLatin, code prints funny characters
    By Nutshell in forum C Programming
    Replies: 16
    Last Post: 01-23-2002, 07:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21