简体   繁体   English

未初始化的指针变量有什么用?

[英]What is an un-initialized pointer variable good for?

I was asked this question in an interview. 采访中有人问我这个问题。

If there is a pointer that is declared but not initialized like this: int *ptr; 如果存在这样声明但未初始化的指针: int *ptr;

Is it assigned a default value? 是否分配了默认值? Or will it be a null pointer? 还是将它为空指针? Also, what happens if we try to use this pointer like: 另外,如果我们尝试使用以下指针,会发生什么情况:

if(ptr) { //block of code }

Will the if condition pass? if条件会通过吗? Also, will this work on debug/release build? 另外,是否可以在调试/发布版本上使用?

I tried to run this program at home and found that if condition passes, and if I try to print the value of the ptr like this: 我试图在家中运行该程序,发现if条件通过,并且如果我尝试打印ptr的值,如下所示:

printf("%x %d", ptr, *ptr);

it prints some random value, but it doesn't crash the program. 它会打印一些随机值,但不会使程序崩溃。 What is the explanation behind this? 这背后的解释是什么?

If it's a global variable (or static ), it will be implicitly initialized to NULL . 如果它是全局变量(或static ),则将其隐式初始化为NULL Otherwise, its value is undefined. 否则,其值是不确定的。 Therefore, the behaviour of that if statement is undefined (ie you can make no assumptions about what it might do). 因此,该if语句的行为是未定义的(即,您无法假设其可能会做什么)。 The behaviour of your second example is also undefined (and it could result in a segmentation fault). 您的第二个示例的行为也是未定义的(可能会导致分段错误)。

Uninitialized pointers are considered dangerous; 未初始化的指针被认为是危险的。 imagine the consequences of this code: 想象一下这段代码的后果:

int *ptr;

...

*ptr = 42;  // Dereference pointer and trash somewhere random in memory

But note that this might not crash; 但是请注意,这可能不会崩溃; it may corrupt things silently. 它可能会默默地破坏事物。

So the convention is to always explicitly initialize it to point at something useful. 因此,惯例是始终显式初始化它以指向有用的东西。 If that's not possible (eg you don't yet have something to point to), the next best thing you can do is initialise it to NULL . 如果那是不可能的(例如,您还没有要指向的东西),那么您可以做的下一个最佳选择就是将其初始化为NULL Dereferencing a NULL pointer will raise a segmentation fault on most platforms, which will at least help you track down the bug. 取消引用NULL指针将在大多数平台上引发分段错误,这至少将帮助您查找错误。

Uninitialized pointers are good for security vulnerabilities and possibly contributing to seeding random number generators. 未初始化的指针有益于安全漏洞,并可能有助于播种随机数生成器。

Your if condition will only skip the block of code if the pointer had been initialized to NULL (0). if指针已初始化为NULL (0), if条件只会跳过代码块。

Because the initialization value is undefined by default it is always considered good practice to initialize to NULL . 由于默认情况下未定义初始化值,因此始终将其初始化为NULL被认为是一种好习惯。

Your printf statement just illustrates the fact that it defaulted to a value other than NULL and in this case it was a valid area of memory that could be dereferenced. 您的printf语句仅说明了它默认为NULL以外的值的事实,在这种情况下,它是可以取消引用的有效内存区域。

What does an un-initialized pointer variable good for? 未初始化的指针变量有什么用?

Efficiency. 效率。 If C automatically initialized pointers to NULL , it would waste a whole CPU instruction (or possibly more if the variable had to be stored in main memory instead of a register), and that was unacceptable to the designers of C. Would you believe programmers these days wanting to sacrifice valuable nanoseconds of runtime just to save a few hours of debugging? 如果C自动将指向NULL指针初始化,则会浪费整个CPU指令(如果变量必须存储在主存储器中而不是寄存器中,则可能会浪费更多的CPU指令),这对于C的设计人员是不可接受的。您是否相信程序员几天想牺牲宝贵的纳秒级运行时间,以节省几个小时的调试时间?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM