snippet code 1

  1. #include <iostream>
  2. int main(int argc, char **argv)
  3. {
  4. std::cout << 25u - 50;
  5. return 0;
  6. }

如果是32位的整型,那么结果是4294967271
Tips:如果进行运算的两个item,lower type将会被转化成为 higher type。类型的高低顺序为: long double, double, float, unsigned long int, long int, unsigned int, int(lowest). 25u - 50便成为 25u - 50u

In C++, if the types of two operands differ from one another, then the operand with the “lower type” will be promoted to the type of the “higher type” operand, using the following type hierarchy (listed here from highest type to lowest type): long double, double, float, unsigned long int, long int, unsigned int, int (lowest). Moreover, the result of the operation will be of the type of the operands. Therefore, the result of 25u - 50u will itself be an unsigned integer as well. So the result of -25 converts to 4294967271 when promoted to being an unsigned integer.

snippet code 2

  1. my_struct_t *bar;
  2. /*... do stuff, including setting bar to point to a defined my_struct_t object ... */
  3. memset(bar, 0, sizeof(bar));

在这里sizeof(bar)计算的是指针bar本身的大小,应该使用的是sizeof(*bar), 如果bar是野指针,那么可以引起 dereferencing error , 因此最安全的方法还是使用 sizeof(my_struct_t)

The last argument to memset should be sizeof(*bar), not sizeof(bar). sizeof(bar) calculates the size of bar (i.e., the pointer itself) rather than the size of the structure pointed to by bar. The code can therefore be corrected by using sizeof(*bar) as the last argument in the call to memset. A sharp candidate might point out that using *bar will cause a dereferencing error if bar has not been assigned. Therefore an even safer solution would be to use sizeof(my_struct_t). However, an even sharper candidate must know that in this case using *bar is absolutely safe within the call to sizeof, even if bar has not been initialized yet, since sizeof is a compile time construct.

snippet code 3

  1. size_t sz = buf->size();
  2. while (--sz >= 0)
  3. {
  4. /* do something */
  5. }

以上的代码可能会无限的循环知道内存 corrupting ,这是因为size_t其实是与机器相关的unsigned integer的引用,因此sz不可能为负。以上的片段可以用如下代码来替代

The problem in the above code is that --sz >= 0 will always be true so you’ll never exit the while loop (so you’ll probably end up corrupting memory or causing some sort of memory violation or having some other program failure, depending on what you’re doing inside the loop).

The reasons that --sz >= 0 will always be true is that the type of sz is size_t. size_t is really just an alias to one of the fundamental unsigned integer types. Therefore, since sz is unsigned, it can never be less than zero (so the condition can never be true).

  1. for (size_t i = 0; i < sz; i++)
  2. {
  3. /* do something */
  4. }

snippet code 4

option1
  1. vector vec;
  2. /*.......*/
  3. for(auto itr = vec.begin(); itr != vec.end(); itr++) {
  4. itr->print();
  5. }

option2
  1. vector vec;
  2. /*.......*/
  3. for (auto itr = vec.begin(); itr != vec.end(); ++itr) {
  4. itr->print();
  5. }

option2优于option1,这是因为itr++比++itr更加的耗费资源。当然,很多编译器会自动把第一种优化成为第二种。