这个问题在这里已有答案:
为什么默认情况下没有使用NULL初始化指针? 15个答案例如,声明引用而不将其分配给某些东西会产生编译器错误,例如。 int &refVar; 但这并不适用于指针。 我得到了c ++跳过一些理智的默认功能,如跳过绑定检查等以获得速度,但这个理论不适合指针变量。
为什么有人会声明一个指针变量而不是将它初始化为什么? 为什么不到NULL / 0甚至nullptr ? 大多数静态分析器实际上指出了这个问题,但是可能的原因是定义这种未定义的行为? 这不是一个很好的错误来源吗?
This question already has an answer here:
Why aren't pointers initialized with NULL by default? 15 answersFor example declaring a reference without assigning it to something gives a compiler error eg. int &refVar; but this doesn't holds true for pointers. I get that c++ skips some sane default features like skipping bound checks etc to gain speed but this theory dosen't fits well with pointer variables.
Why would someone declare a pointer variable and not initialize it to something? And why not to NULL/0 or even nullptr? Most static analyzers actually point out this problem, but what could be the possible reason to define this undefined behaviour? Isn't this a great source of bugs?
最满意答案
我认为很可能是由于初始化这些变量可能产生的开销。 对于一个变量,这可能不是一个大问题,但想象一下在堆栈上分配的数组。
I think most likely it is due to possible overhead of initializing these variables. This might not be such a big problem for one variable, but imagine arrays allocated on the stack.
更多推荐
发布评论