Home arrow C++ arrow Page 3 - 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? - 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.
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