看完后隐藏功能和C++/STL的暗角上comp.lang.c++.moderated,我完全惊讶的是,下面的代码片断编译并在两个Visual Studio 2008和G ++ 4.4的工作.
这是代码:
#include <stdio.h>
int main()
{
int x = 10;
while (x --> 0) // x goes to 0
{
printf("%d ", x);
}
}
Run Code Online (Sandbox Code Playgroud)
我假设这是C,因为它也适用于GCC.标准中定义了哪里,它来自何处?
我很好奇解除引用NULL ptr的哪一部分会导致不良行为.例:
// #1
someObj * a;
a = NULL;
(*a).somefunc(); // crash, dereferenced a null ptr and called one of its function
// same as a->somefunc();
// #2
someObj * b;
anotherObj * c;
b = NULL;
c->anotherfunc(*b); // dereferenced the ptr, but didn't call one of it's functions
Run Code Online (Sandbox Code Playgroud)
在这里我们在#2中看到我实际上并没有尝试从b中访问数据或函数,所以如果*b刚解析为NULL并且我们将NULL传递给anotherfunc(),这仍会导致意外行为吗?