Why sizeof (this - id) returns 8?

I had a very silly typo in my code ...

is.read((char*)&this->id, sizeof(this-id));

missing character >afterthis-

Interestingly, sizeof(this - id)8 is back!

What I think ... since it thisis a pointer, doing a subtraction on thiswill result in another pointer that is disabled by the id value, which can be anything, depending on the id value.

And ... on a 64-bit system, a pointer is usually 8 bytes!

Am I right? or is something missing?

Below is the class that I have.

class IndexItem : public Serializable {
public:
  IndexItem(uint32_t id, std::streampos pos) :
    id(id),
    pos(pos)
  { }
  uint32_t id;
  std::streampos pos;
protected:
  std::ostream& Serialize(std::ostream& os) const override {
    os.write((char*)&this->id, sizeof(this->id));
    os.write((char*)&this->pos, sizeof(this->pos));
    return os;
  }

  std::istream& Deserialize(std::istream& is) override {
    is.read((char*)&this->id, sizeof(this->id));
    is.read((char*)&this->pos, sizeof(this->pos));
    return is;
  }
};
+4
source share
1 answer

. , sizeof(this-id)=sizeof(this), 8.

+8

Source: https://habr.com/ru/post/1598988/


All Articles