C++
  Home arrow C++ arrow Page 2 - Pointers In C - A Boon Or A Bane?
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

Pointers In C - A Boon Or A Bane?
By: Deepak P
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 26
    2002-08-19

    Table of Contents:
  • Pointers In C - A Boon Or A Bane?
  • Pointers In C
  • Dangling Pointers
  • Matrices Are Pointers Too!
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More C++ Articles
    More By Deepak P


     

    C++ ARTICLES

    - Multiplying Large Numbers with Karatsuba`s A...
    - Large Numbers
    - Dijkstra`s Shunting Algorithm with STL and C...
    - Brief Introduction to the STL Containers
    - The Standard Template Library
    - Templates in C++
    - C++ Programmer Alerts
    - C++ Programming Tips
    - First Steps in (C) Programming, conclusion
    - First Steps in (C) Programming, continued
    - First Steps in (C) Programming, introduction
    - C++ Preprocessor: Always Assert Your Code Is...
    - C++ Preprocessor: The Code in the Middle
    - Programming in C
    - Temporary Variables: Runtime rvalue Detection







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway