What Are Wild Pointers? How Can We Avoid It

Share Your Love

Wild pointers are uninitialized pointers that point to any arbitrary memory location, potentially causing a program to crash or behave improperly.

int main()
{
  int *p; /* wild pointer */
  /* Some unknown memory location is being corrupted.
     This should never be done. */
  *p = 12;
}

It’s important to remember that a pointer p that points to a known variable is not a wild pointer. In the programme below, p is a wild pointer until it reaches a.

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

We should explicitly allocate memory and put the value in allocated memory if we want a reference to a value (or set of values) without having a variable for the value.

int main()
{
   int *p = (int *)malloc(sizeof(int));
   *p = 12; /* This is fine (assuming malloc doesn't return NULL) */
}

If you want to improve this post please connect with us.

Share Your Love
Avatar photo
Lingaraj Senapati

Hey There! I am Lingaraj Senapati, the Founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.

Articles: 355

Newsletter Updates

Enter your email address below to subscribe to our newsletter