Pointers In C - A Boon Or A Bane? - Pointers In C (Page 2 of 5 )
C supports pointers and almost entirely relies on pointers for every non-scalar variable type. An array is actually a pointer and so is a matrix. You can declare a pointer with the following syntax:
... where A is a primitive type like int, char etc and a is the instance of a pointer to it. You can set it to point to a location like this:
... where b is to type A. After this operation, *a is actually the r-value of b and a is the l-value of b. Pointers can prove to be very handy at times. Can you just imagine what the following code does in MS-DOS mode?:
It simply reboots the machine and MS-DOS does allow it to work as it does not have the sophisticated protection features that Windows has -- you can easily access any region of memory. But obviously that would not help security and the job of the operating system to ensure security in multiprogramming systems any easier.
In DOS, the features such as lack of good protection functionalities have been abused in the creation of viruses and TSRs (Terminate and stay resident programs).
Pointers are often an efficient tool for resizing data structures and for explicit heap allocation of memory at runtime. We often declare a pointer and issue a system call such as malloc() or calloc() (in C) to gain some heap memory, but often programmers are not careful enough to deallocate that memory using the free() function. What happens in this scenario? In small programs this does not cause a huge problem. However, in larger programs, we might find ourselves short of memory.
Where did the memory go? It is still held in some location that is no longer accessible by the program? For example:
In code such as that shown above where we lose control of memory we allocated, there can be abnormal program crashes due to memory de-allocation failures that appear almost always transparent to the programmer during his exhaustive debugging session.