Sunday, 6 June 2021

Advantages and disadvantages of pointers in C

A pointer is a variable which holds the memory address of another variables. Pointers are more useful to handle complex data structures like liked list and trees.

Advantages of pointers:

  1. Pointers are more efficient in handling arrays and data tables.
  2. Pointers permit references to functions and there by facilitating passing of function as arguments to other functions.
  3. The use of pointer array to character strings results in saving of data storage space in memory.
  4. Pointers allows us to support dynamic memory management.
  5. They increase the execution speed of the program.


 

Disadvantages of pointers:

Some common errors encountered when using pointers are 

1) Uninitialized error 

Ptr is a pointer pointing to an unknown memory location. Hence it will be over write the location content and sotre 10 in it. such pointer is called wild pointer.

Example: int a=10;

               int *ptr=10;  // generates an error because instead of address we are storing value.

2) Memory leak

Memory leak occurs when memory us allocated but not released when it is not required.

3) Dangling pointer

Dangling pointer occurred when an object is deleted and it is used by some other objects.

Example:

int a=10;

int *ptr=&a;

int *ptr1=ptr;

free(ptr);

In the above example, we are trying to delete pointer "ptr" which still used by another pointer "ptr1", this type of usage causes dangling pointer.

4) Memory corruption

memory corruption often occurs when due to programming errors, the content of a memory location gets modified unintentional 

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.

Find Us On Facebook

Computer Basics

More

C Programming

More

Java Tutorial

More

Data Structures

More

MS Office

More

Database Management

More
Top