Reputation: 3655
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
Reputation: 58848
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