为什么c ++没有默认初始化[重复](why c++ doesn't have default initialization [duplicate])

编程入门 行业动态 更新时间:2024-10-15 00:19:05
为什么c ++没有默认初始化[重复](why c++ doesn't have default initialization [duplicate])

这个问题在这里已有答案:

为什么默认情况下没有使用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 answers

For 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.

更多推荐

本文发布于:2023-07-15 17:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1116663.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:初始化   default   duplicate   initialization

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!