为什么数据实例默认不为 NULL?

发布于 2024-10-31 11:20:05 字数 77 浏览 3 评论 0原文

一直困扰我的事情是,如果没有指定其他内容,为什么编译器不将数据设置为 NULL?我看不出有什么理由不这样做,而且它肯定比其他提到的垃圾更好?

Something that has always bothered me is why isn't data set to NULL by the compiler, if nothing else was specified? I can't see any reason NOT to, and surely it must be better than the junk that gets referred to otherwise?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

暖树树初阳… 2024-11-07 11:20:05

最初的原因是“为了速度”——只有在您为变量分配某些内容后,这些变量才有意义,因此我们努力避免对 0 进行无意义的初始化 无论如何,程序员都会在几毫秒内用实际数据覆盖它。

虽然它并不完全昂贵,但出于同样的原因,编译器现在可能不会将变量清零:与基准测试中的其他编译器相比,任何这样做的编译器都会显得很慢。

The original reason was "for speed" -- the variables were only going to make sense once you assigned something to them, so there was an effort to not make a pointless initialization to 0 that was just going to be overwritten by the programmer in a few milliseconds with the actual data anyway.

While it isn't exactly expensive, compilers probably don't zero out variables today for the same reason: any compiler that does would look slow compared to other compilers on benchmarks.

墨离汐 2024-11-07 11:20:05

唯一好的理由是它效率不高。 C 和 C++(与高级语言不同)往往不会做任何您没有要求的事情。这就是他们如何获得速度极快的声誉的原因。其他语言更安全,但速度较慢。

如果您想将字段初始化为 NULL 以外的任何值,那么首先将其初始化为 NULL,然后然后将其初始化为您设置的任何值,这将是浪费时间。所以 C/C++ 假设你知道自己在做什么。

The only good reason is that it's not as efficient. C and C++ (unlike higher-level languages) tend not to do anything you didn't ask for. That is how they got the reputation of being extremely fast. Other languages are safer, but slower.

If you wanted to initialise fields to anything other than NULL, it would be a waste of time to first initialise it to NULL and then initialise it to whatever you set it to. So C/C++ assumes you know what you are doing.

枫以 2024-11-07 11:20:05

我的猜测是因为速度原因。它们将是另一个 CPU 调用来完成它。

My guess is because of speed reasons. They would be another CPU call(s) to do it.

浪推晚风 2024-11-07 11:20:05

将分配的内存设置为零并将结构标记为“null”需要时间。您的应用程序可能有时间关键的部分,其中必须尽快实例化对象,然后以尽可能简单的方式初始化它们以使它们可用。因此,编译器和运行时在创建对象时所做的工作尽可能少,并让您根据需要完成设置。

一些高级语言确实实现了对“null”的进一步初始化,但这些语言并不总是适合时间关键的应用程序。

Setting the allocated memory to zeroes and marking the structures as 'null' takes time. Your application may have time-critical sections where objects must be instantiated as quickly as possible, after which you initialise them in as minimalist way as possible to make them usable. So the compiler and runtimes do as little work as possible to make the objects, and leave it to you to complete their setup as you see fit.

Some high-level languages do implement further initialisation to 'null', but these are then not always suitable for time-critical applications.

夜光 2024-11-07 11:20:05

如果您在堆上声明一个变量(自由存储,即全局并且仅全局),则该变量将被初始化为 0。

但是,堆栈上的数据不是分配作为加速这一过程的一种方式。如果要覆盖数据,则将数据初始化为 0 是没有意义的,这是对本地(堆栈)数据所做的假设。

If you declare a variable on the heap (not free store, i.e. globally and only globally), this will be initialised to 0.

Data on the stack, however, is not allocated as a way to speed this up. There's no point initialising data to 0 if you're going to overwrite it, which is the assumption made with local (stack) data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文