1

I am trying to verify that an instance of a class will fit in a queue (defined by a C library), and noticed that sizeof() didn't do what I thought it did.

#include <iostream>
using namespace std;

class A {
    int x, y, z;
public:
    A() {}
    void PrintSize() { cout << sizeof(this) << endl; }
};

class B : public A {
    int a, b, c;
public:
    B() {}

    void PrintSize() { cout << sizeof(this) << endl; }
};

int main() {
    A a;
    B b;
    a.PrintSize(); // is 8
    b.PrintSize(); // is 8

    cout << sizeof(a) << endl;
    cout << sizeof(b) << endl;
}

returns:

8
8
12
24

I did not expect the "8" values - why is it different when taken within the class? How can verify that the class that I define fits within a memory space, especially at compile time?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Mike
  • 51
  • 5
  • 4
    `this` is a pointer, it will always have pointer size. Try `sizeof(*this)` or `sizeof(A)`/`sizeof(B)`. – Yksisarvinen Oct 18 '22 at 21:57
  • 1
    Possible duplicates: [Type of 'this' pointer](https://stackoverflow.com/questions/6067244/type-of-this-pointer), [Do all pointers have the same size in C++?](https://stackoverflow.com/questions/71870205/do-all-pointers-have-the-same-size-in-c) – Yksisarvinen Oct 18 '22 at 21:59
  • `sizeof(this)` means return the size of the variable named `this` (yes, it is just an address, or known as a pointer). And since `this` variable takes only 8 bytes, it shows the result of 8. – Xin Cheng Oct 18 '22 at 22:01
  • 8 is because sizeof class with no members is default 8 on this 64bit compiler, I think. I thought that sizeof would include the non-static members though. – Mike Oct 18 '22 at 22:01
  • 1
    @Mike No, it's because you take the size of pointers just as Yksisarvinen mentioned and Vlad has answered below. Just do `sizeof *this` (no parentheses needed in a `sizeof expression`) and you'll get the size of the object `this` is pointing at. – Ted Lyngmo Oct 18 '22 at 22:02
  • @Mike `sizeof` class with no members is `1` (assuming no `virtual` functions etc). You take `sizeof` a pointer, and sizeof pointer type doesn't depend on any members or the actual pointed-to type. – Yksisarvinen Oct 18 '22 at 22:02
  • This question had nothing to do with pointers, the ANSWER does, but not the question! – Mike Oct 18 '22 at 22:05
  • 1
    @Mike Your question is literally all about that. _"I did not expect the "8" values - why is it different when taken within the class?"_ - It's not different when taken within the class. It's because you take the size of the wrong thing inside the class - and that's the answer. – Ted Lyngmo Oct 18 '22 at 22:06
  • 1
    `this` is a pointer. Type of `this` in `A::PrintSize()` is `A*`, same for `this` in `B::PrintSize()` - `B*`. Your question has everything to do with pointers. – Yksisarvinen Oct 18 '22 at 22:06
  • Obviously I forgot that this is a pointer, but I would need to realize that before I could look for that question. – Mike Oct 18 '22 at 22:11
  • No need to look for that question, you have a *pointer* to it now. – Mark Ransom Oct 19 '22 at 01:50
  • 1
    Ha ha! I see what you did there. Thanks for _this_ comment – Mike Oct 19 '22 at 14:27

1 Answers1

4

This expression

sizeof(this)

gives the size of the pointer this. It seems you mean the size of the corresponding class

sizeof( *this )
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Thank you for your answer, it is a very helpful response. – Mike Oct 18 '22 at 22:07
  • @Mike [Some extra documentation on `this`](https://en.cppreference.com/w/cpp/language/this). Mostly it's the kind of stuff you don't need to know until suddenly it's important, but if you know it before it becomes important, you can save yourself a ton of time. – user4581301 Oct 18 '22 at 22:13