This blog is under construction

Sunday 22 December 2013

Wild Pointers

Key points on wild pointers:
  • If any of the pointer is uninitialized, then that pointer is called wild pointer.
  • Wild pointers won't refer to any valid memory location.
  • Dereferencing wild pointer would cause segmentation fault.

Consider the following, 
int func() {
int *iptr;
char *cptr;
}
Here, iptr and cptr are wild pointers.  Because, they are not initialized to NULL.

int func() {
static int *int_ptr;
char *char_ptr;
}
Here, int_ptr and char_ptr are not wild pointers.  Because, they are static variables.  By default, static variables are initialized to 0.

Below is the example C program on wild pointer.

  #include <stdio.h>
  int main() {
        int *ptr;  // wild pointer
        if (ptr) {
                printf("ptr is not NULL\n");
                *ptr = 10;
                printf("*ptr: %d\n", *ptr);
        }
        return 0;
  }



  Output: (Wild pointer example)
  jp@jp-VirtualBox:~/$ ./a.out
  ptr is not NULL
  Segmentation fault


1 comment:

  1. Dell Laptop Repair Center in Noida is no.1 service center which provides door to door services in or its nearby areas. We have expert, technicians who can repair your laptop at your home. . Call us: 9891868324

    ReplyDelete