C++
  Home arrow C++ arrow Page 3 - 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  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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 / 27
    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? - Dangling Pointers


    (Page 3 of 5 )

    There is another problem -- that of dangling pointers ( or dangling references). What are they? They are pointers to locations that are no longer meaningful:

    void function(int *b)
    {
    int a=10;
    b=&a;
    return;
    }


    This might appear to be very much correct for the beginner, but a is a dynamic stack variable that is de-allocated automatically on return from the function. So when the control returns to the caller of this function, the pointer b always points to garbage. Such problems are always not obvious to the beginners.

    Strings in C are also basically pointers, hence there might be problems there also. You cannot directly initialize one string to another. Instead, you have to make use of the library function defined in string.h called strcpy(), which duplicates the content of one string to a destination string, for example:

    char a[]="deepak";
    char b[]="india";
    a=b;


    In this code above we lose the area of memory where we stored the string "deepak" and it cannot be used for any other purpose as it is still under control of the programmer from the system point of view (or rather, it has not been de-allocated). This is a very common problem that causes a loss of huge amounts of time in a typical C debugging session.

    Due to the need for explicit de-allocation for every byte of heap memory allocated, we are actually compelled to perform the memory management tasks manually. This is also the case with C++ and Pascal. In this era when nobody wants to spend an extra second of time than what is needed, the school of thought that the programmer has to work at a higher level has gained upper hand and many technologies were proposed to deal with this low level work automatically.

    One of such techniques called garbage collection has gained widespread acceptance. It is being used in JAVA. Here we allocate any amount of memory and do not de-allocate it -- the system does it for us. This was carried on to C# but then the C# gurus perhaps were very much fond of pointers that they included this wonderful provision: label a part of code as unsafe and the system will not do any automatic memory management there. You can label such small parts of code (including functions) as unsafe so that pointers may be used safely within 'unsafe' code.

    Garbage Collection
    But then how can JAVA manage these things without pointers? It has something like a clone of pointers called references. It does not hold any meaningful value until it is allocated in memory. There should be other consequences of "pointerlessness", and indeed there are: You cannot pass a scalar by reference (to a function). All objects are allocated from the heap and not from the stack. And worst of them all, the garbage collector comes into play only when the program is short of memory.

    What does this mean exactly? Well, the garbage collector works in a small amount of memory and hence works the slowest when it is called. "When you need it the most, it works the worst" says R.W Sebesta in his book the "Concepts of Programming languages".

    int * function()
    {
    int a[10];
    /* Values filled in a */
    return a;
    }


    Does the code shown above seem okay to you? Think twice! The value returned from the function is garbage.

    What exactly does it convey? You can return a vector only when it is non-local from a function, but then a little common sense is enough to understand that non-local returns are not necessary available when storing data.

    Is this a limitation? It surely is when you consider that the only way of conveying a string to the caller is to store it in a non-local area that is accessible also to the caller.

    More C++ Articles
    More By Deepak P


     

    C++ ARTICLES

    - 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++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek