Home arrow C++ arrow Page 2 - Pointers In C - A Boon Or A Bane?
C++

Pointers In C - A Boon Or A Bane?


Pointers have been known to cause hours upon hours of frustration for C/C++ programmers, but are they good or bad? Here's Deepak's Insight...

Author Info:
By: Deepak P
Rating: 3 stars3 stars3 stars3 stars3 stars / 31
August 19, 2002
TABLE OF CONTENTS:
  1. · Pointers In C - A Boon Or A Bane?
  2. · Pointers In C
  3. · Dangling Pointers
  4. · Matrices Are Pointers Too!
  5. · Conclusion

print this article
SEARCH DEVARTICLES

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:

A *a;

... 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:

a= &b;

... 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?:

main()
{
void interrupt(*old)();
old=0XFFFF0000;
(*old)();
return 0;
}


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:

void function()
{
int *a=(int *)malloc(1000*sizeof(int));
// ......
// ......
return;
}


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.
blog comments powered by Disqus
C++ ARTICLES

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials