Thread: sizeof(structure)

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    385

    sizeof(structure)

    sizeof(int) 4 + sizeof(float) 4 + sizeof(char) 1 = 9 bytes, then why does this code report the size of structure p as 12 bytes????
    Code:
    #include<stdio.h>
    
    int main(void)
    {
    	struct
    	{
    		int a;
    		float b;
    		char c;
    	}p;
    	
    	printf("Size of float = %d, Size of int = %d, size of char = %d\n",sizeof(float),sizeof(int),sizeof(char));
        printf("Size of structure = %d\n",sizeof(p));     
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by juice
    sizeof(int) 4 + sizeof(float) 4 + sizeof(char) 1 = 9 bytes, then why does this code report the size of structure p as 12 bytes????
    Read: Why does sizeof report a larger size than I expect for a structure type, as if there were padding at the end?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    17
    It is becuase of structure padding.. just reffer to structure padding concept in net... you will get the solution

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    If you want to remove the padding, try the __packed__ flag:

    Code:
    	struct
    	{
    		int a;
    		float b;
    		char c;
    	} __attribute__((__packed__)) p;
    You can do this over the entire struct, or just on specific variables if the need arises.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    52
    Wow this is a great site laserlight! Thank you for the link!

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by memcpy View Post
    If you want to remove the padding, try ...
    That's unresponsible to straight out show how to work around the padding without first saying why it is there and why it is probably better that it stays there.
    Not only that, but the way you've shown is highly compiler dependent. Visual Studio for one, does it a different way.

    ushacy, the padding is there for performance reasons, or in the case of certain CPUs, it's required for alignment when an array of that type is used. Taking away the padding will most likely at least result in slower code and possibly a crash.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 12-09-2010, 02:33 PM
  2. Replies: 4
    Last Post: 04-25-2010, 10:57 AM
  3. sizeof applied to structure...
    By roaan in forum C Programming
    Replies: 26
    Last Post: 08-01-2009, 01:09 AM
  4. Replies: 6
    Last Post: 10-15-2007, 08:05 AM
  5. sizeof(cin) and sizeof(cout)
    By noobcpp in forum C++ Programming
    Replies: 11
    Last Post: 06-30-2007, 11:00 AM