Water
Water

Reputation: 3655

Is sizeof converted to a constant number after compilation?

Are these equal speed-wise?

return someNumber / sizeof(myStruct); // Pretend sizeof returns 88 always.

and

return someNumber / 88;

I'm unsure if the compiler calculates sizeof every single time or writes a constant (thus making it safe for me to include sizeof() instead of a constant).

Upvotes: 1

Views: 122

Answers (1)

Yes. The compiler knows sizeof(myStruct) at compile-time, and will replace it with the appropriate constant.

This is always true for sizeof expressions in C++.

Note: this means that, for example, sizeof(a++) will not increment a.

Upvotes: 8

Related Questions