Wild Pointer

Wild pointers are uninitialized pointers that point to any random position in memory while being unassigned from any other memory location. This could occasionally result in a programme crashing or acting erratically.

For instance:

int *ptr;

A pointer called ptr is generated in this example, but it is empty. The pointer ptr is now a wild pointer as a result. Declaring a pointer without initialising it has drawbacks of its own. One such drawback is that it will save any worthless information in it. It will hold in it arbitrarily a position in memory. This random allocation frequently becomes difficult for a programmer to debug, leading to several issues with the program’s operation.

A. Avoiding problems due to WILD pointers

In order to avoid problems that can arise while dereferencing a wild reference, we frequently opt to change a void pointer to a NULL pointer. By doing this, our pointer will instead point to a NULL position rather than any trash memory location. Simply setting a wild pointer equal to NULL will make it a NULL pointer.

B. Dereferencing

A wild pointer cannot be dereferenced since we are unsure of the data it is pointing at in memory. Dereferencing a wild pointer can result in numerous problems as well as a software crash.

Example:

#include <bits/stdc++.h>
using namespace std;

int main() {
   int *arr;
   for(int i=0; i<5 ; i++)
   cout << arr[i] << ” “;
   return 0;
}

Output:1 0 -426634956 32764 0

Shubhajna Rai
Shubhajna Rai

A Civil Engineering Graduate interested to share valuable information with the aspirants.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in