Uninitialized pointers are known as wild pointers because they point to some arbitrary memory location and may cause a program to crash or behave badly.
view source
print?
int main()
{
int *p; /* wild pointer */
*p = 12; /* Some unknown memory location is being corrupted. This should never be done. */
}
Please note that if a pointer p points to a known variable then it’s not a wild pointer. In the below program, p is a wild pointer till this points to a.
view source
print?
int main()
{
int *p; /* wild pointer */
int a = 10;
p = &a; /* p is not a wild pointer now*/
*p = 12; /* This is fine. Value of a is changed */
}
If we want pointer to a value (or set of values) without having a variable for the value, we should explicitly allocate memory and put the value in allocated memory.
view source
print?
int main()
{
int *p = malloc(sizeof(int));
*p = 12; /* This is fine (assuming malloc doesn't return NULL) */
}
Popular Posts
-
Hello, You can play with your linux machine. It can talk for you what you want. May be you know the Linux utility espeak. It can be used t...
-
1. If u born poor it is not ur mistake, BUT if u die poor it is ur mistake ....................................................................
-
Uninitialized pointers are known as wild pointers because they point to some arbitrary memory location and may cause a program to crash or b...
-
Do Your best...Let the life do the rest..
-
What are these? Little and big endian are two ways of storing multibyte data-types ( int, float, etc). In little endian machines, last byte ...
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment