简体   繁体   English

声明结构时,总是将其设置为NULL?

[英]is a ponter to struct always set to NULL when declared?

I'm declaring a pointer to a struct. 我在声明一个指向结构的指针。 When I'm printing its value it's not set to NULL and I get random numbers. 当我打印它的值时,它没有设置为NULL并且得到了随机数。 Is this normal? 这正常吗? You just need to look at the printf on line 3. I don't even use this pointer. 您只需要查看第3行上的printf 。我什至不使用此指针。 I declared it just to see what happens because I was getting wrong values. 我声明它只是为了看看会发生什么,因为我得到了错误的值。 I don't have any other pointers with the same name, and this function is being called correctly. 我没有任何其他具有相同名称的指针,并且此函数正在正确调用。

child * find_closer(int cid,child * root){
child *closer,*right,*left,*min,*not;
if(not!=NULL)
printf("    %d   ",not);
if(root!=NULL){
    if(root->cid < cid)
        closer=root;
    else
        min=root;
    if(root->lc!=NULL){
        if(closer==NULL || (root->lc->cid  <  cid  && closer->cid < root->lc->cid))
            closer=root->lc;
        if(min==NULL || min->cid > root->lc->cid)
            min=root->lc;
    }
    if(root->rc!=NULL){
        if(closer==NULL || (root->rc->cid  <  cid  && closer->cid < root->rc->cid))
            closer=root->rc;
        if(min==NULL || min->cid > root->rc->cid)
            min=root->rc;
    }
    if(closer==NULL){
        closer=min;
    }
    right=find_closer(cid,root->rc);
    left=find_closer(cid,root->lc); 
    if(closer->cid < cid){ 
        if(right!=NULL &&right->cid < cid && right->cid > closer->cid)
            closer=right;
        if(left!=NULL && left->cid < cid && left->cid > closer->cid)
            closer=left;
    }
    if(closer->cid<cid){
        return closer;
    }else{
        if(right!=NULL &&closer->cid > right->cid)
            closer=right;
        if( left!=NULL && closer->cid > left->cid)
            closer=left;
        return closer;
    }
}else
return NULL;

} }

The only declarations guaranteed to be zeroed out in C are global and static declarations. 保证在C中被清零的唯一声明是全局和静态声明。 If you declare a variable in a function, it will be uninitialised. 如果在函数中声明变量,则该变量将未初始化。

No, local pointers to struct, like the ones in your code sample, are not initialized to NULL on declaration. 不,与声明中的代码类似,结构的本地指针在声明时不会初始化为NULL

Anything uninitialized can be treated as having "garbage" or "junk" contents. 任何未初始化的内容都可以视为具有“垃圾”或“垃圾”内容。 So in the case of your uninitialized child pointer, it contains a random memory address (could be NULL , could be 0xDEADBEEF , could be anything). 因此,对于未初始化的child指针,它包含一个随机的内存地址(可以为NULL ,可以为0xDEADBEEF ,可以为任何值)。

Uninitialized pointers are dangerous to dereference. 未初始化的指针对取消引用很危险。 Best case you could segfault. 最好的情况是您可能出现段错误。 Worst case, you accidentally access memory that belongs to your process, possibly modifying some local variable, which can result in some nasty bugs to track down. 最坏的情况是,您不小心访问了属于您进程的内存,可能修改了一些局部变量,这可能会导致一些令人讨厌的错误以进行跟踪。

So yes, it is normal to see seemingly random contents in uninitialized variables. 所以是的,在未初始化的变量中看似随机的内容是正常的。 Always initialize pointers before you dereference them. 在取消引用指针之前,请务必先对其进行初始化。 Contrast this to a language like java which has auto initialization features. 将此与具有自动初始化功能的Java之类的语言进行对比。

Accessing uninitialized variables in Undefined Behavior . 未定义行为中访问未初始化的变量。

When you declare the pointer to struct , they are not initialized to NULL . 当声明pointer to structpointer to struct ,它们不会初始化为NULL


NOTE 注意

Static and global variables are initialized automatically to 0 . 静态变量和全局变量自动初始化为0

If the pointer to your struct is global or a static variable, then it would be initialized to NULL. 如果指向您的结构的指针是全局或静态变量,则它将被初始化为NULL。 The local (automatic) variable defined inside a function cannot be guaranteed to NULL initialized. 在函数内部定义的局部(自动)变量不能保证初始化为NULL。 Thus it is always a good practice to explicitly initialize such variables. 因此,显式初始化此类变量始终是一个好习惯。

In C, if you declare any variable as global or static or declared in heap using calloc , values are zero but if it is declared as local to any function, they are uninitialized. 在C语言中,如果将任何变量声明为全局变量或静态变量,或使用calloc在堆中声明,则值均为零,但如果将其声明为任何函数的局部变量,则它们不会被初始化。 Now it seems that you used the printf in the main function which means all the pointers are declared locally and they are storing some garbage values. 现在看来,您在main函数中使用了printf ,这意味着所有指针都在本地声明,并且它们存储了一些垃圾值。 Hence when you de-reference the pointer, you get the value stored in that garbage location. 因此,当取消引用指针时,您将获得存储在该垃圾位置的值。 But keep in mind that this value is not known to anybody and hence it a bad practice to keep them uninitialized. 但是请记住,任何人都不知道此值,因此,不初始化它们是一种不好的做法。 You should always declare it with NULL 您应该始终将其声明为NULL

If you write NULL or 0 or nullptr in C++11 , it is guaranteed to give segmentation fault versus if you leave it uninitialized, they may alter/access memory mapped for some other process which in some platform can cause the raise of SIGSEGV signal but while in some other platforms (insure ones) may pose a security threat. 如果您在C ++ 11中写入NULL0nullptr ,则可以保证会产生分段错误,而如果不对其进行初始化,则它们可能会更改/访问为某些其他进程映射的内存,这在某些平台上可能导致SIGSEGV信号升高但是在某些其他平台(保险平台)中却可能构成安全威胁。 Hence the best practice is to initialize them with NULL. 因此,最佳实践是使用NULL初始化它们。

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

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