Wild Pointer in C Language with Example Programs

wild-pointer-in-c-programming-language

Introduction:

We have looked at the void pointer in c in an earlier article. In today’s article, We will look at the wild pointer in c programming language.

📢 This Program is part of the Pointers Practice Programs series

What is a Wild pointer in C language:

A Wild pointer is a pointer variable, that doesn’t point to a valid memory address. The uninitialized pointer variables are a good example of a Wild pointer in C.

Let’s look at an example of the wild pointer in c language.

Here the wPtr pointer is a wild pointer as we haven’t initialized it with any address.

Accessing Wild Pointer in C:

Accessing the wild pointers will result in undefined behavior. It might introduce bugs in the program and Sometimes the program might crash as well.

In the above example, We tried to access the value pointed by the wPtr. As the wPtr is a wild pointer, It will result in undefined behavior.

Program to understand the Wild Pointer in C Language – What happens if we access wild pointer:

Here is a program to demonstrate the behavior of the wild pointer and how a wild pointer can cause the program crashes.

In the above program, We have created an integer pointer wPtr and not initialized it. So the wPtr is currently has a garbage value i.e It is pointing to an invalid memory location. Then we tried to print the value pointed by the wild pointer wPtr

Program Output:

Let’s Compile the program.

We used the gcc compiler to compile the program and it generates the a.out executable file.

what-happens-if-we-access-wild-pointer-in-programming

As we can see from the above output, The Dereferencing of the wild pointer wPtr ( *wPtr)caused the Segmentation fault.

The Segmentation fault occurs when you try to access the memory which is not allocated to you. So here the wild pointer wPtr is pointing to some garbage location, So we got the segmentation fault and the program crashed💥.

How to Properly Handle Wild Pointer in C Language?

So it is important to check if the pointer is holding the valid address before trying to dereference the pointer (Access value).

Try to Initialize the pointer variables with appropriate values:

We can initialize the pointer variables with the NULL character instead of simply declaring the value. and check the pointer with NULL before dereferencing the pointer variable.

Initialize the pointer variable with a NULL value.

While accessing the ptr, check if the ptr is not equal to NULL.

Only access the pointer ptrvalue if contains a valid address of any variable.

Properly Handle Wild Pointer in C – Initialize and Check Pointers before accessing it:

Now, In the following program, We initialized the pointer variable wPtr with the NULL value. While accessing or dereferencing the wild pointer wPtr, We are going to check if the pointer still has the NULL address. If it is still NULL, Then the pointer doesn’t hold any valid address, So we can’t dereference it or use it.

Program Output:

Let’s compile and run the program and observe the output.

how-to-avoid-crashes-with-wild-pointer-in-c

As we can see from the above output, The wild pointer wPtr doesn’t updated and still has the value NULL, We simply printed a warning message Warning: Pointer is Not Initialized!, This way we can avoid program crashes.

If the pointer variable wPtr is indeed updated with the value of a variable, Then the wPtr won’t be equal to the NULL, So the if condition will execute and user gets the proper value.

Related Pointer Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

1 Response

  1. […] Wild Pointer in C Language with Example Programs […]

Leave a Reply