“野指针"是什么意思?在 C? [英] What is the meaning of "wild pointer" in C?

查看:24
本文介绍了“野指针"是什么意思?在 C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我,野指针在C中的含义,如何获得它,这在C++中是否可用?

Can anybody tell me, the meaning of wild pointer in C, how to obtain it and is this available in C++?

推荐答案

该标准没有定义或使用术语野生".我会小心地纠正"其他人对其含义的看法,我特别避免引用随机的非规范互联网垃圾来支持我的立场.

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.

这篇关于“野指针"是什么意思?在 C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆