开发者

What is the meaning of "wild pointer" in C?

Can anybody tell me, the mea开发者_如何学编程ning 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.

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
   }
}  

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.


A wild pointer in C is a pointer that has not been initialised prior to its first use. From Wikipedia:

Wild pointers are created by omitting necessary initialization prior to first use. Thus, strictly speaking, every pointer in programming languages which do not enforce initialization begins as a wild pointer.

This most often occurs due to jumping over the initialization, not by omitting it. Most compilers are able to warn about this.

eg

int f(int i)
{
    char* dp;    //dp is a wild pointer
    ...
}


It is not s standard term. It is normally used to refer to the pointers pointing to invalid memory location. int *p; *p = 0; //P is a wild pointer

Or

int *p = NULL;
{
  int a;
  p = &a; // as soon as 'a' goes out of scope,'p' is pointing to invalid location
}

*p = 0;


To get a wild (aka dangling) pointer you:

  • Create an object
  • Create a pointer to that object
  • Delete the object
  • Forget to set the pointer to null

The pointer is now classed as "wild" as it's pointing to an arbitrary piece of memory and using it in this state could cause problems with your program.


A pointer which is not initialized with any address is called as a wild pointer. It may contain any garbage address, so dereferencing a wild pointer is dangerous


A wild pointer is any pointer that is used (particularly as an L_value {ie (*pointer) = x } ) while having a value that is either not correct or no longer correct. It could also be used to describe the using of memory that is not defined as a pointer as a pointer (possibly by having followed a wild pointer or by using outdated structure definitions).

There's no official definition. It's just words that we use when referring to certain pointer errors or the results of those errors.


Wiki

It's a pointer to either uninitialized object or an object with a bad state. using this pointer will cause trouble. the wiki is a good source of explanation.


Wild pointer is a pointer that doesn’t point to either a valid object (of the indicated type, if applicable), or to a distinguished null value, if applicable.

Read more about Wild Pointer here


Wild pointer is a pointer which declaration is present but it has not been defined yet.Means we have declare a pointer - data_type *ptr; //but not define which address it is containing *ptr=100//wild pointer does not point to any valid address.so we will get ERROR printf("ptr:%d",ptr);//we will get:0(in gcc compiler)


a ponter which not have locating any data type varable that varable is call the wild pointer


Uninitialized pointer is called Wild Pointer. Suppose if you try

int *p; //pointing to any random or unknown location. *p= 16; /Some unknown memory location is being corrupted.This should never be done./

This can create a great threat to your program. B'Coz because they point to some arbitrary memory location and changing the content of that location may cause a program to crash or behave badly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜