且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

什么是&QUOT意义;野指针"用C?

更新时间:2022-05-29 08:34:28

标准没有定义或使用术语野。我会小心纠正意味着什么其他人的意见,我特别避免乱报价不规范互联网的垃圾来支持我的立场。

The standard does not define or use the term "wild". I'd be careful "correcting" other people's opinions about what it means, and I'd especially avoid quoting random non-normative internet junk to support my position.

要我来说,这将意味着一个指针既不是指合法的对象,也不是NULL。这些类型的指针值的可能来源可能包括未初始化的指针的对象,已经不再存在的对象,计算指针值,正确对齐的指针值,指针本身,还是它指出,意外损坏等。

To me, it would mean a pointer that neither refers to a legitimate object, nor is NULL. Possible sources of these types of pointer values might include uninitialized pointer objects, objects that have ceased to exist, computed pointer values, improperly aligned pointer values, accidental corruption of the pointer itself, or what it pointed to, etc.

int main(void)
{

   int *p;  // uninitialized and non-static;  value undefined
   { 
      int i1; 
      p = &i1;  // valid 
   }            // i1 no longer exists;  p now invalid    

   p = (int*)0xABCDEF01;  // very likely not the address of a real object

   { 
      int i2;  
      p = (int*)(((char*)&i2) + 1);  // p very likely to not be aligned for int access
   }

   {
      char *oops = (char*)&p;  
      oops[0] = 'f';  oops[1] = 35;  // p was clobbered
   }
}  

和等等,等等。有各种各样的方式来获得一个无效的指针值C.我最喜欢的一定是谁试图通过写他们的地址到一个文件中拯救他的目标的人。奇怪的是,当他在程序的不同运行过程中读取这些指针值,他们并没有指出他的对象了。花式,那个。

and so on, and so forth. There are all kinds of ways to get an invalid pointer value in C. My favourite has got to be the guy who tried to "save" his objects by writing their addresses to a file. Strangely, when he read back those pointer values during a different run of the program, they didn't point to his objects any more. Fancy, that.

不过,这正是野生意味着我。因为它不是一个规范术语,它意味着无论谁说话或写这意味着它的意思的人。问他或她。

But that's just what wild means to me. Since it's not a normative term, it means whatever the person who spoke or wrote it meant it to mean. Ask him or her.