Loading....
Coupon Accepted Successfully!

 

wild Pointers

Pointers are used to store memory addresses. An improper use of pointers creates many errors in the program. Hence, pointers should be handled cautiously. When a pointer points to an unallocated memory location or to a data value whose memory is de-allocated, such a pointer is called a wild pointer. The wild pointer generates garbage memory location and pendent reference.

When a pointer pointing to a memory location vanishes, the memory is transformed into garbage memory. It indicates that a memory location exists but a pointer is destroyed. This happens when the memory is not de-allocated explicitly.
 
The pointer becomes a wild pointer due to the following reasons:
  1. Pointer declared but not initialized
  2. Pointer alteration
  3. Accessing destroyed data
  1. When a pointer is declared and not initialized, it holds an illicit address. It is very hard to manipulate such a pointer. Consider the following example:

13.8 Write a program to use wild pointer.

#include<iostream.h>

#include<conio.h>

int main()

{

clrscr();

int *x;

for (int k=0;k<10;k++)

cout<<x[k]<<“ ”;

return 0;

}

OUTPUT

28005 27760 29793 29541 29728 8303 25954 28704 25205 26988


Explanation:
In the above program, pointer x is declared and not initialized. Using for loop, the location-containing pointer is increased, and successive addresses are displayed.
  1. The careless assignment of a new memory location in a pointer is called pointer alternation. This happens when another wild pointer accesses the location of a legal pointer and converts the legal pointer into a wild one. Consider the following program:
  2. Sometimes, the pointers attempt to access the data that no longer have life. The following program illustrates this:

13.9 Write a program to display the output when a pointer is accessing a temporary data of the memory.

#include<iostream.h>

#include<conio.h>

char*instring();

char*inchar();

void main()

{

clrscr();

char *ps,*pc;

ps=instring();

pc=inchar();

cout<<“ String : ”<<*ps<<endl;

cout<<“ Character : ”<<*pc<<endl;

}

char *instring()

{

char str[]= “cpp”;

return str; }

char *inchar()

{

char g;

g=‘D’;

return &g; }

OUTPUT

String : c

Character : .


Explanation:
In the above program, ps and pc are character pointers. The function instring() and inchar() returns references (base address of the string or character), and they are stored in the pointers ps and pc, respectively. When the control exits from these functions and returns to main(), local variables inside the user-defend functions are destroyed. Thus, the pointers ps and pc point to the data that are destroyed. The contents displayed of the pointers will be as shown in the output.




Test Your Skills Now!
Take a Quiz now
Reviewer Name