C:为什么未分配的指针指向不可预测的内存,而不指向NULL?(C: Why do unassigned pointers point to unpredictable memory and NOT p

编程入门 行业动态 更新时间:2024-10-27 03:35:57
C:为什么未分配的指针指向不可预测的内存,而不指向NULL?(C: Why do unassigned pointers point to unpredictable memory and NOT point to NULL?)

很久以前,我曾经在C上上课。 我记得我真的很讨厌C:未分配的指针不指向NULL。

我问了许多人,包括老师为什么在世界上他们会使未指定的指针默认行为不指向NULL,因为它似乎更危险的是不可预测的。

答案据说是表演,但我从来没有买过。 我认为编程历史上的许多错误可以避免,因为C默认为NULL。

这里有一些C代码指出(双关语)我在说什么:

#include <stdio.h> void main() { int * randomA; int * randomB; int * nullA = NULL; int * nullB = NULL; printf("randomA: %p, randomB: %p, nullA: %p, nullB: %p\n\n", randomA, randomB, nullA, nullB); }

其中编译有警告(很高兴看到C编译器比我上学时更好)并输出:

randomA:0xb779eff4,randomB:0x804844b,nullA :((nil),nullB:(nil)

A long time ago I used to program in C for school. I remember something that I really hated about C: unassigned pointers do not point to NULL.

I asked many people including teachers why in the world would they make the default behavior of an unassigned pointer not point to NULL as it seems far more dangerous for it to be unpredictable.

The answer was supposedly performance but I never bought that. I think many many bugs in the history of programming could have been avoided had C defaulted to NULL.

Here some C code to point out (pun intended) what I am talking about:

#include <stdio.h> void main() { int * randomA; int * randomB; int * nullA = NULL; int * nullB = NULL; printf("randomA: %p, randomB: %p, nullA: %p, nullB: %p\n\n", randomA, randomB, nullA, nullB); }

Which compiles with warnings (Its nice to see the C compilers are much nicer than when I was in school) and outputs:

randomA: 0xb779eff4, randomB: 0x804844b, nullA: (nil), nullB: (nil)

最满意答案

其实这取决于指针的存储。 具有静态存储的指针由空指针初始化。 具有自动存储持续时间的指针未初始化。 见ISO C 99 6.7.8.10:

如果具有自动存储持续时间的对象未被显式初始化,则其值是不确定的。 如果没有明确初始化具有静态存储持续时间的对象,那么:

如果它有指针类型,则将其初始化为空指针; 如果它具有算术类型,则将其初始化为(正或无符号)为零; 如果是聚合,每个成员都按照这些规则进行初始化(递归); 如果它是一个联合,则根据这些规则初始化(递归地)第一个命名的成员。

是的,由于性能原因,自动存储持续时间的对象未初始化。 只要想象一下,每次调用一个日志记录函数(在我工作的一个项目中看到的东西),最好让我避免初始化,从而导致一个很好的性能提升)。

Actually, it depends on the storage of the pointer. Pointers with static storage are initizalized with null pointers. Pointers with automatic storage duration are not initialized. See ISO C 99 6.7.8.10:

If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static storage duration is not initialized explicitly, then:

if it has pointer type, it is initialized to a null pointer; if it has arithmetic type, it is initialized to (positive or unsigned) zero; if it is an aggregate, every member is initialized (recursively) according to these rules; if it is a union, the first named member is initialized (recursively) according to these rules.

And yes, objects with automatic storage duration are not initialized for performance reasons. Just imagine initializing a 4K array on every call to a logging function (something I saw on a project I worked on, thankfully C let me avoid the initialization, resulting in a nice performance boost).

更多推荐

本文发布于:2023-07-31 01:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340399.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不   指针   分配   内存   NULL

发布评论

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

>www.elefans.com

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