Advantages and disadvantages of pointers in c

Benefits(use) of pointers in c:

  • Pointers provide direct access to memory
  • Pointers provide a way to return more than one value to the functions
  • Reduces the storage space and complexity of the program
  • Reduces the execution time of the program
  • Provides an alternate way to access array elements
  • Pointers can be used to pass information back and forth between the calling function and called function.
  • Pointers allows us to perform dynamic memory allocation and deallocation.
  • Pointers helps us to build complex data structures like linked list, stack, queues, trees, graphs etc.
  • Pointers allows us to resize the dynamically allocated memory block.
  • Addresses of objects can be extracted using pointers

Drawbacks of pointers in c:
  • Uninitialized pointers might cause segmentation fault.
  • Dynamically allocated block needs to be freed explicitly.  Otherwise, it would lead to memory leak.
  • Pointers are slower than normal variables.
  • If pointers are updated with incorrect values, it might lead to memory corruption.
Basically, pointer bugs are difficult to debug.  Its programmers responsibility to use pointers effectively and correctly.

0 nhận xét:

Đăng nhận xét