0

This is printing 16 and 4 as the answer but it should be printing as 8 and 4 because both a and b are integer type variables. So can we think this as the compiler stores the address of the variable a in a separate variable while copying to b that is why it is resulting in 4 + 4 + 8 = 16? If not then what is it?

#include <iostream>
using namespace std;

class C {
public:
  int a = 45;
  int &b = a;
};

int main() {
  C ob1;
  cout << sizeof(ob1) << endl;
  cout << sizeof(ob1.b);

  return 0;
}
0xdw
  • 3,755
  • 2
  • 25
  • 40
wholesome
  • 57
  • 9
  • "but it should be printing as 8 and 4 because" why? What makes you think the size has to be 8 and 4? Why it cannot be 107 and 42? – 463035818_is_not_an_ai Nov 10 '20 at 15:25
  • 1
    this https://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member + this https://stackoverflow.com/questions/10446907/why-reference-size-is-always-4-bytes-c should answer your question – 463035818_is_not_an_ai Nov 10 '20 at 15:28
  • 1
    Note that `sizeof` of a reference will produce the size of the referred type. References do not have a size you can directly determine. – François Andrieux Nov 10 '20 at 15:29
  • @idclev 463035818 it is because the object ob1 has two data variable and both of int data type of size 4 each..am i wrong? – wholesome Nov 10 '20 at 15:29
  • @user55092 A reference most likely requires `sizeof(void*)` bytes of storage. `sizeof(obj.b)` is `sizeof(int)` not the actual size of the reference member. – François Andrieux Nov 10 '20 at 15:30
  • 1
    yes wrong. `sizeof` a struct is not necessarily sum of sizeof its members. And sizeof(reference) yields size of what is referenced – 463035818_is_not_an_ai Nov 10 '20 at 15:30
  • @user55092 Yes, you are wrong. C++ doesn't defines to size of int has to be 4. On 64 systems it is likely to be 8. – gerum Nov 10 '20 at 15:30
  • also, common sense of compiler writers aside there is no guarantee that sizeof a `struct foo { int x;};` is the same or anywhere close to `sizeof(int)` – 463035818_is_not_an_ai Nov 10 '20 at 15:31
  • @user55092 -- Is there a reason why you are expecting the size to be what you think it should be? If you're just curious as to the reason why there is a difference, ok, but if it's for some other reason, maybe you should state what that reason is. – PaulMcKenzie Nov 10 '20 at 15:37
  • 1
    Technically a reference has no size whatsoever, but this is really, really hard to do in practice, so the reference is probably hiding a pointer (usually 8 bytes these days). Getting 4 for the `sizeof` an `int` reference says an `int` is 4 bytes on your system. So we have 4 bytes, then probably 8 bytes, but we can't necessarily [align](https://stackoverflow.com/questions/381244/purpose-of-memory-alignment) 8 bytes after 4 bytes so we get 4 bytes, 4 more bytes of padding and then 8 bytes = 16 bytes. – user4581301 Nov 10 '20 at 15:39
  • @PaulMcKenzie No I am just curious why it is showing 16 what is the exact reason behind it. I am not that much sure that it should show 8 but why 16? – wholesome Nov 10 '20 at 15:46
  • Can't really give an exact reason, but two comments up is my educated guess at what's going on. The two links by idclev463035818 even further up are instrumental for understanding what's going on. – user4581301 Nov 10 '20 at 15:47
  • @user4581301 that explanation sounds good thank you. – wholesome Nov 10 '20 at 15:48
  • @user55092 Ok. Usually the reason why persons are curious as to the difference in size is that they relied on the size being `x` instead of `y`, maybe for some network packet or binary data reading / writing issues. – PaulMcKenzie Nov 10 '20 at 15:50
  • side note: references in `struct`s and `class`s can be hard to get right. You can't change what a reference references so assigning a `class` containing a reference is tricky. Usually it's more trouble than it's worth and you wind up falling back to a pointer. – user4581301 Nov 10 '20 at 15:52
  • @PaulMcKenzie so does the 16 comes due to padding or any other reason? – wholesome Nov 10 '20 at 15:52
  • There could also be hidden, implementation-specific information in there, but that usually only crops up with polymorphic structures. – user4581301 Nov 10 '20 at 15:55

0 Answers0