Never do unary minus on sizeof

This tip submitted by Vaibhav on 2006-01-15 02:13:54. It has been viewed 32151 times.
Rating of 5.7 with 237 votes



What will be output of
cout<<-sizeof(char);


No, it's not -1, it is 4294967295!

The return type of sizeof is size_t (similar to unsigned int). If you try to use -sizeof() you can now understand what will happen. Once I used it in seekg() function and spent nearly 6 hours debugging it before I finally spotted it.



More tips

Help your fellow programmers! Add a tip!