-3
int main()
{
  struct node
  {
    char i;
    int a;
  };
  printf("sizeof(struct node) =%d\n",sizeof(struct node));
  return 0;
}

The output for this program is 8 byte. Here sizeof(int)+size(char) is not equal to 8 byte. Still, we are getting 8 byte. It is because of padding. Thats fine. But in the following program, this very concept is being violated. Why?

int main()
{
  struct node
  {
    double i;
    int a;
  };
  printf("sizeof(struct node) =%d\n",sizeof(struct node));
  return 0;
}

if sizeof(double) is 8 byte then sizeof(struct node) should be 16 byte(as per first program). But it is printing 12 byte. Why?

user3686233
  • 109
  • 1
  • 1
  • 6
  • 3
    Well, your `int`s appear to have 4 bytes and 8+4 is 12… Also, use `%zu` for `size_t`. – mafso Jul 23 '14 at 11:03
  • @mafso: have a look on this link. http://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member – user3686233 Jul 23 '14 at 11:05
  • 2
    why you don't check how large is your data with sizeof(double)+sizeof(int). After that sizeof(node) should be >= sizeof(int)+sizeof(data). – Klaus Jul 23 '14 at 11:05
  • but padding and object alignment are usually followed in C. Why is it not being followed here? – user3686233 Jul 23 '14 at 11:06
  • Why you think it should be 16? – mafso Jul 23 '14 at 11:06
  • because of padding and object alignment... @mafso – user3686233 Jul 23 '14 at 11:08
  • 4
    Your machine seems to feel comfortable with 4-byte alignment. Why you expect the padding in the first place (you cannot assume padding to occur, just like you cannot assume it not to, generally)? – mafso Jul 23 '14 at 11:09
  • Padding is inserted only when rounding to divisors of 4, i.e. it is machine dependent. – Igor Pejic Jul 23 '14 at 11:10
  • Igor: could you please give me some link for the same? – user3686233 Jul 23 '14 at 11:13
  • http://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member?lq=1 – Igor Pejic Jul 23 '14 at 11:18
  • In your target platform, it is enough that the `double` is -byte aligned only, even though it requires 8 bytes of memory. Thus the struct does not need to be 16 bytes. Now if the `double`s were preferred to be 8-byte aligned, then the size could be 16 as seen by some answers. – Antti Haapala -- Слава Україні Jul 23 '14 at 15:11

3 Answers3

2

I tried it on my system, and it returns 16 even if i define a as a char. That's because the padding is machine-dependant.

Shlomi Agiv
  • 1,183
  • 7
  • 17
0

First of all size of data type and structure padding depends on your machine architecture as well as compiler you are using.

Structure allocates the memory is sum of all the elements size of structure. In case of 32 bit int is 4 byte and double is 8 byte. What i think is you are running on a 32 bit machine thats why you are getting 4(int) + 8(double) = 12 byte .

if you will run on a different architecture,output will differ according the size of data types and structure padding.

Usr1
  • 369
  • 1
  • 6
  • 15
0

This happens due to byte alignment and padding which let the structure comes out to 12 bytes (or words) on your 32-bit (possibly) platform.

For example, if I use this:

struct first 
{
  int a;
  int b;
}first;

struct second 
{
  int a:4;
  int b:8;
}second;

In first, when I use sizeof(struct first), it will give an output of 8 (i.e. 2 X 4-Bytes Integers). But in the second case, instead of 12 bits (4+8), it gives 4 as a value as the second structure is padded so that it becomes a single word (in a 32-bit system).

Shashish Chandra
  • 489
  • 2
  • 5
  • 20