0

I was trying to solve one exam question, where question was asked that what is the sizeof class A and class B?

class A {
    char c;
    short s;
    char c2;
    int i;
};
class B {
    int i;
    short s;
    char c, c2;
};

As per me

sizeof class A : 8 (char:1, short:2, char:1, int:4 -- No Padding)

sizeof class B : 8 (int:4, short:2, char:1+1 -- No Padding)

When I tried in my system then I got different output than what I was expecting

sizeof class A : 12

sizeof class B : 8

When I made some changes in the class

class A {
    char c,c2;
    short s;
    //char c2;
    int i;
};
class B {
    int i;
    short s;
    char c, c2;
};

Then Output was sizeof(A): 8 and sizeof(B): 8

Can someone please explain me that how output is 12 & 8? Thanks in advance

van neilsen
  • 547
  • 8
  • 21
  • `class A{ char c; char padding1[1]; short s; char c2; char padding3[3]; int i;}`. so `short` has address multiple of 2, and `int` multiple of 4. – Jarod42 Nov 21 '17 at 17:01
  • This is a duplicate, but in a quick search I haven't found the question it duplicates. The compiler inserts padding to meet the members' alignment requirements. – Keith Thompson Nov 21 '17 at 17:02
  • @PasserBy That's I was assuming that there wont be any padding and the size will be **8 & 8** respectively. But when I checked in my system then I got output **12 & 8**. How **12 & 8** that is creating confusion. – van neilsen Nov 21 '17 at 17:02
  • You tagged this question `padding`. The documentation for the `padding` tag pretty much contains the answer to your question. The amount of padding that the compiler inserts into a struct declaration will depend on the target architecture and on the compile-time options. – Solomon Slow Nov 21 '17 at 17:02
  • 2
    @vanneilsen *what is the sizeof class A* -- Answer: `sizeof(A)` -- *and class B?* -- Answer: `sizeof(B)`. – PaulMcKenzie Nov 21 '17 at 17:03
  • Duplicate: https://stackoverflow.com/questions/4306186/structure-padding-and-packing – Passer By Nov 21 '17 at 17:03
  • @KeithThompson I tried searching this issue on internet where I can get the solution but I dint get. So at the end I posted it here. – van neilsen Nov 21 '17 at 17:03
  • 2
    The correct answer is "the sizes are implementation defined". Whoever set up the exam may or may not know or expect this answer. – n. m. could be an AI Nov 21 '17 at 17:04
  • class A will definitely have lots of padding on most platforms with common compilers. Just think of the first two members - to allign the `short` after the `char`; padding will be needed. – Jesper Juhl Nov 21 '17 at 17:04
  • @PasserBy That is not the same question https://stackoverflow.com/questions/4306186/structure-padding-and-packing because there it is clear char:1 +3(padding) , int:4 , char:1+3(padding). So the size is 12. But in my case it is different – van neilsen Nov 21 '17 at 17:07
  • @PaulMcKenzie When I checked in system then output was *sizeof(A): 12* and *sizeof(B): 8* – van neilsen Nov 21 '17 at 17:08
  • How is it different? Are you looking for a word-for-word exact copy of your exam question? There will be no such thing. The problem is explained thoroughly in both linked posts – Passer By Nov 21 '17 at 17:09
  • @vanneilsen -- Also, you assumed that an `int` is 4 bytes. Were you told on the question what is `sizeof(int)`? If not, then even if you assumed no padding, the answer is still `sizeof(A)` since you were not told what `sizeof(int)` is. – PaulMcKenzie Nov 21 '17 at 17:09
  • @PasserBy I checked that link and the question which is asked and the explanation. But If you look at the question which I asked here and you find it duplicate because you find some link. Please you check the question clearly then you will get to know that both will differ in lot when it comes to padding. If I wrong can you explain me that why output is 12 ? – van neilsen Nov 21 '17 at 17:23
  • @PaulMcKenzie sizeof(int): 4 – van neilsen Nov 21 '17 at 17:23
  • @PasserBy I have explained my doubt as well properly in my question. char:1, short:2, char:1, int:4 means total:8 . Then in that case there wont be any padding. – van neilsen Nov 21 '17 at 17:26
  • I don't understand. What do you mean there won't be any padding? There is padding. What you see is a result of padding. What about the links didn't explain what you saw is the result of padding? You still didn't explain __how__ they are different – Passer By Nov 21 '17 at 17:28
  • @bolov can you explain me that how this post is duplicate? Both question which I asked and the link which you have posted is no where linked. If it is linked can you tell me the answer that why output is 12? – van neilsen Nov 21 '17 at 17:30
  • @PasserBy As per your link: short:2+2(padding), int: 4, char: 1+3(padding) – van neilsen Nov 21 '17 at 17:32
  • @vanneilsen -- So the compiler is wrong? You added up the `sizeof()` of each member, and came up with 8. The compiler comes up with 12. So how do you explain why the compiler came up with 12? Is it magic? Is it because the compiler is broken? Or could it be -- *padding*? The only logical conclusion is that it is due to padding. A broken compiler? I don't think so. – PaulMcKenzie Nov 21 '17 at 17:37
  • @PasserBy As per your link: short:2+2(padding), int: 4, char: 1+3(padding) But in my case if you see char:1, short:2, char:1, int:4 . I am understanding that from where I am getting padding. Is that padding from 1st char or short or 2nd char? I hope you got my point. I thought I will get solution but when you make my post as *duplicate*, what ever responses I was getting all stopped. It will be better if you can explain why I am getting output as **12**. Because if you have this post *duplicate* then you will be having better understanding than me. Thanks – van neilsen Nov 21 '17 at 17:41
  • __Read__ the duplicates. They answer __everything__ – Passer By Nov 21 '17 at 17:43
  • @PaulMcKenzie Yes, it is due to padding correct. That's what my question is ,that from where this padding is getting added, is that by 1st char or short or 2nd char? If you can clear doubt it be great. Thanks – van neilsen Nov 21 '17 at 17:44
  • @PasserBy I read that link but I dint find the solution. That's what I am asking to you, that if you have the clarity can you answer that in my question which datatype is having padding? – van neilsen Nov 21 '17 at 17:48
  • @Jarod42 Yes , that might be the case. Thanks – van neilsen Nov 21 '17 at 18:00
  • 1
    This example should explain what's going on. The `short` and `int` members require padding to satisfy their alignment requirements. https://pastebin.com/w8PcmpWN – Keith Thompson Nov 21 '17 at 21:21
  • @KeithThompson Now it is clear from your link. Thanks – van neilsen Nov 22 '17 at 05:57

0 Answers0