What is the meaning of "wild pointer" in C? - Stack Overflow
language agnostic - null pointer vs dangling pointer - Stack Overflow
wild pointer
c++ - what is the Different categories of pointers? - Stack Overflow
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
...
}
Pointer terminology:
- Dangling (or wild) pointer: a pointer that points somewhere, but not to a valid object.
- Null pointer: a pointer that points to a specially designated out-of-bounds location that programs will never legally store data in. Special class of dangling pointer.
- Uninitialized pointer: a pointer that was never assigned to the address of something. A type of dangling pointer.
- Stale pointer: a pointer that used to point to something, but the target has been deleted (either via delete operator, free, or gone out of scope). A type of dangling pointer.
Pointers that are dangling can be said to be pointing to Hyperspace or to Another Dimension, except the null pointer, which is generally referred to as pointing to Nothing.
A null pointer just means the pointer isn't pointing to anything, or in some languages means it is unknown what it is pointing at. But because it is a null pointer, you know this, the code knows this, so no problem. A dangling pointer is one that you think is pointing at something but in reality is no longer there, hence the pointer is actually inaccurate but doesn't know it.
{
char *dp = NULL;
/* ... */
{
char c;
dp = &c;
}
/* c falls out of scope */
/* dp is now a dangling pointer */
}
In many languages (e.g., the C programming language) deleting an object from memory explicitly or by destroying the stack frame on return does not alter associated pointers. The pointer still points to the same location in memory even though that location may now be used for other purposes.
wikipedia
so what is the problem if this address allocated with the same or different data type again
Q :
is that the same thing
#include <iostream>
int main(){
int x=4;
int *i=&x;
char *c=(char*)&x;
bool *b=(bool*)&x;
} - Valid pointer: one pointing to a real object in memory
- Invalid pointer: one pointing to memory that is not what it is supposed to be.
- NULL pointer: A pointer whose value is 0 and thus points at nothing.
- Dangling pointer (also sometimes wild pointer): one pointing to memory that has been deleted/freed.
- Smart pointer: Not really a pointer at all, but rather an object which acts like a pointer, but manages the memory for you.
There are also function pointers, which point to code rather than objects. And pointers to members, which require an instance of a class to fully dereference. And pointers to member functions, which point to code and requires and an instance of a class to fully dereference.
There are several different types of smart pointers as well. These are used to wrap up dynamic memory allocations, and track ownership of the underlying data. When nobody owns the data any more, the dynamic memory is automatically released. The three biggies here are scoped pointers, shared pointers, and weak pointers. Scoped pointers only have one owner, and when they go out of scope they get deleted [see std::auto_ptr in C++03 and std::unique_ptr in C++0x]. Shared pointers can have many owners, and the memory doesn't get freed until all the owners are done with them. Weak pointers are a form of these, but they don't maintain strict ownership at all times; they request ownership when they need it, and that request is denied if the corresponding shared pointer has run out of owners.