Thread: struct padding question..

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    145

    struct padding question..

    Hi All,


    Code:
     #include<stdio.h>
     
    struct node
    {
       char p;
       char c;
       char d;
        //int a;      //---------------------- Number 1
    };
    
    int main()
    { 
    	struct node a;
             
        printf("size of node a:%d",sizeof(a));
    	getch();
        return 0;
    }
    Here the Output is

    3

    Questions

    1. If only char variables are present how will the processor access the memory address?
    Assuming processors with fewer pins?

    2. Shouldn't the variables be aligned at boundaries multiples of 2? What are criteria's for alignment of variables at even boundaries(basically when all)?

    3. Introduction of Number 1 line will change the entire picture and structure is padded?

    Thanks in advance
    Last edited by sanddune008; 07-15-2010 at 01:03 AM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There are no standard answers for this. The compiler is responsible for padding and will do so at its own discretion.
    Any x86 processor can access unaligned memory, though doing so may incur a speed hit. If the compiler chooses it, it may align them on a boundary of 2, 4 or 8 or an even higher number.
    But the rules are specific to compilers and differ between them, so the questions are pointless.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Some compilers allow #pragma pad instructions to force alignment to 2, 4, 8 byte boundaries so the programmer is given some flexibility.
    It would be prudent for the compiler to default to an alignment granularity that corresponds to the current machine's native addressing, i.e. 32-bit, 64-bit. But if the executable is portable, that criteria is not going to ensure optimum performance either.

    Even if in your example the size of the struct is 3, I'm sure it's still aligned to some power-of-2 address. You may want to declare a second struct and see the address of where it's stored. I bet it's not 3 bytes from the first one.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by nonoob View Post
    Even if in your example the size of the struct is 3, I'm sure it's still aligned to some power-of-2 address. You may want to declare a second struct and see the address of where it's stored. I bet it's not 3 bytes from the first one.
    I bet it is, actually. The compiler needs to ensure correct alignment, but it also doesn't want to waste memory pointlessly. If you declare an array of two of those, and take sizeof() on the array, I bet it's simply twice the size. Single chars don't need an alignment of more than 1, at least on any reasonable architecture.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C problem - Create a Personal Library
    By Harliqueen in forum C Programming
    Replies: 33
    Last Post: 04-20-2010, 11:27 PM
  2. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  3. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  4. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  5. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM