Thread: What does this struct look like in memory?

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    16

    What does this struct look like in memory?

    Code:
    struct student {
    	char id;
    	char *name;
    	int grade;
    };
    
    int main ()
    {
    	struct student school[2];
    	
    	school[0].id = 1;
    	school[0].grade = 92;
    	school[0].name = "roberto clemente";
    	
    	school[1].id = 2;
    	school[1].grade = 76;
    	school[1].name = "bobby hillenbrand";
    	
    	return 0;
    }
    When I messed around with it myself, printing addresses, etc., I found that school[1] was 24 bytes after school[0] and that, within each struct, each element was 8 bytes away from the next... even though I have different data types such as char, char*, and int.

    This leads me to believe that, within the structure, each element is actually a pointer to the value (obviously I would expect this of the element char *name, but not the others)... Is this incorrect? Because it seems awful weird.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Now compare that to:
    Code:
    char foo;
    char *bar;
    int baz;
    By themselves. Is 'bar' one byte away from 'foo'? Maybe.

    Your compiler is probably aligning the items in the structure to fall on word boundaries. You can generally force them to be packed, to not be padded, but it's a bit slower to access the members that way. You can also play around with offsetof.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    16
    Ah I see.
    Then in this case, sizeof(struct whatever) probably doesn't take into consideration the types of data in the struct but rather how many elements there are?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    sizeof( struct foo ) takes the size of the space in memory that the structure takes due to alignment of the number of elements.

    If they have padding, then you'll see bigger than if you just add up: sizeof( char ) + sizeof( char* ) + sizeof( int ).


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    30
    Quote Originally Posted by quzah View Post
    sizeof( struct foo ) takes the size of the space in memory that the structure takes due to alignment of the number of elements.

    If they have padding, then you'll see bigger than if you just add up: sizeof( char ) + sizeof( char* ) + sizeof( int ).
    Exactly!

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by steez View Post
    Ah I see.
    Then in this case, sizeof(struct whatever) probably doesn't take into consideration the types of data in the struct but rather how many elements there are?
    The sizeof struct objects is determined by two kinds of padding:

    - Internal padding to align every struct member on its natural boundary, and
    - Tail padding to make sizeof struct a multiple of the sizeof its most restrictive type.

    id can be located anywhere; name and grade will be located at an address divisble by 4, and
    the size of the struct is a multiple of 4 as that's the sizeof its most restrictive member type.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Putting A Struct Into Shared Memory
    By Lockout in forum C Programming
    Replies: 1
    Last Post: 02-28-2011, 03:05 AM
  2. Struct memory issues
    By Chris87 in forum C Programming
    Replies: 6
    Last Post: 02-12-2009, 08:35 AM
  3. memory alignment of a struct
    By George2 in forum C Programming
    Replies: 4
    Last Post: 04-04-2007, 12:34 PM
  4. delete memory in a struct destructor
    By JeremyCAFE in forum C++ Programming
    Replies: 5
    Last Post: 03-12-2006, 11:34 AM
  5. struct memory
    By cboard_member in forum C++ Programming
    Replies: 2
    Last Post: 12-19-2005, 12:04 PM