tags:

views:

799

answers:

8

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

+1  A: 

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.

Dani
If an object violates its invariants, it can also be said to bein a bad state, but a pointer to it isn't a wild pointer. So that definition is somewhat wrong.
sbi
It depends what is the nature of the bad state. But I see your point, I have referred to the wiki page for the exact definition.
Dani
Note that the language requires that access through an lvalue of type unsigned char can always be used to read or write to an object, even if it is in a bad state when interpreted as its declared type.
janks
ie, for any object, regardless of current state, this is legal:`unsigned char *safe = (void*)``/* now access through safe[0]...safe[sizeof some_object - 1]``is legal, safe, well-defined, even if some_object has an invalid value */`
janks
+5  A: 

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;
Naveen
When the location that pointer is pointing to goes out of scope (or is freed), it becomes dangling pointer, not wild. Wild pointer is one that is not initialized.
Technowise
@Technowise: I'd disagree. I've heard these two terms used interchangeably.
sbi
+9  A: 

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
    ...
}
Amal Sirisena
The term is also used for a pointer which _had_ been initialized, but which becomes a wildiesis dead by now. see Naveen's answer: http://stackoverflow.com/questions/2583656/2583678#2583678
sbi
@Magnus - Thanks for the edit!@sbi - I have usually heard pointers in the state you described being called "dangling pointers", but you are correct that they are effectively in the same state as a "wild pointer".
Amal Sirisena
@Amal not quite the same - in many systems with dynamic memory, the data a dangling pointer points at will appear to be valid until it is reused, whereas a wild one will point to garbage.
Pete Kirkham
+3  A: 

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.

Paolo
I think you have to call them **Dangling pointers**, the *wild* ones are the unitialized ones
Eineki
+1 for mentioning the term "dangling pointer". I suspect though that a dangling pointer's not a wild pointer, as Eineiki says.
Frank Shearar
`void* p;` also creates a "wild pointer".
sbi
The standard does not define "wild" or "dangling", so these corrections are dubious at best. The standard uses the terms "valid" and "invalid".
janks
@sbi: only at block scope (ie, automatic storage duration). Put that definition at file scope, and it is required to be initialized to NULL.
janks
+2  A: 

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: 

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

sam
+34  A: 

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.

janks
+1 for pointing out the the term is imprecise and giving 4 possible reasons.
Steve Fallows
+1  A: 

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.

nategoose