Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pointers? 1

Status
Not open for further replies.

totti

Technical User
Jan 24, 2003
1
IT
Hello, I just started learning c++ and i don't see why you should use pointers.
For example, I have the following sourcecode:

char c = 'a';
char* p = &c;
char c2 = *p;
std::cout << c2;

this isn't useful is it? because you can also write :

char c = 'a';
std::cout << c;

and than you have the exact same outcome as the source with the pointer in it.
I heard that pointers save memory but i think there must be another reason to use them, I just don't know which reason.
Can someone please tell me the reason why to use pointers?
 
In your example, pointers don't provide much, that's the reasoning behind Java. Where pointers really shine is doing things like complex classes, vectors, lists, etc. IMHO, for much programming (all beginning programming?) pointers aren't really necessary, but time and again, Java programmers doing complex programming have to find work arounds for lack of pointers.


James P. Cottingham
 
One example: passing large sized parameters to a function.

Suppose you have defined a class
TMyClass{

...

}
with a big number of attributes inside. Each instance (object) of this class takes up a large place in memory (let's say 60 bytes).

Now you want to design a function of prototype:

void Func(TMyClass Object);

which takes a TMyClass object as argument.

It means that you will provide Func with a full copy of Object on the stack (60 bytes).


The alternative is to design your function as

void Func(TMyClass *Object);


This means that you provide Func with the adress in memory (4 bytes) where it can find the original TMyClass instance.

Especially if your function is to be called repeatedly many times, you are gaining a lot of efficiency by storing 4 bytes on the stack instead of 60, each time Func is called. In effect, your program is quicker.


This is a quite trivial reason why pointers are used, but not the only one.

You can also think of interesting data structures: storing an array of adresses (pointers) to objects, instead of an array of objects; chained lists ...


Hope to be of any help!

Cordially,

Woliwol
 
All of the above answers are good examples of pointers. Her is another, pointers in C replace global variables.
This is desirable because C doesn't correct you if you screw up. Example:
You have a global variable called x. Any part of your program can act on x. Good right? Not in C. C was designed to comparmentalize programing. that means that if you import someone else's code that happens to use x it will also act on your global friend. This can be very hard to debug. So we have pointers. Consider:

void main( void )
{
int x;
x = 10;
myfunction(x); // Value of x is passed.
x=x+10; // x will equal 20 here
}
void myfunction( int x )
{
x=x+5; // x will equal 15 but only in myfunction()
return;
}
Now with a pointer:

void main( void )
{
int x;
x = 10;
myfunction(x); // Pointer to x is passed.
x=x+10; // x will equal 25 here
}
void myfunction( int *x )
{
x=x+5; // x will equal 15 and return it to main()
return;
}
 
Now with a pointer:

void main( void )
{
int x;
x = 10;
myfunction(x); // Pointer to x is passed.
x=x+10; // x will equal 25 here
}
void myfunction( int *x )
{
x=x+5; // x will equal 15 and return it to main()
return;
}


should be changed to

void main( void )
{
int x;
x = 10;
myfunction([COLOR=00FF00]&[/color]x); // Pointer to x is passed.
x=x+10; // x will equal 25 here
}
void myfunction( int *x )
{
[COLOR=00ff00]*[/color]x=[COLOR=00ff00]*[/color]x+5; // x will equal 15 and return it to main()
[COLOR=00ff00]// return not needed[/color]
return;
}


but the same result can be achieved by passing by reference. Pointers are more applicable in the cases stated above (i.e. a large class) BUT they are really useful in polymorphism.

Lets say you have a base class Shape and two classes that derive from Shape (lets call these classes Circle and Rectangle).

Now Shape has base functions like &quot;drawShape&quot; and &quot;paintShape&quot;... Lets say you dont know what &quot;Shape&quot; to draw initially but you want to determine it dynamically. You could do something like:

Shape* getShape(<some parameters that will determine type>)
{
if(param == &quot;Circle&quot;)
return new Circle;
else
return new Rectangle;
}

so now somewhere else we say

Shape* myShape = getShape(<params>);
myShape->drawShape();
delete myShape;

here it is very abstracted but you drew the right shape (whether rectangle or circle) and you didnt need to declare the specific type because Rectangle and Circle have an &quot;IS_A&quot; relationship with shape. This is a very basic example which doesn't really unleash all the power of the pointer but gives an easy to understand example of how they could be applied.

Matt
 
Everything constributed here is very good and useful for
U to understand what Pointers are and why should they be
used ? I will tell U something very important which U should
never forget.

1. EVERY TIME U ALLOCATE MEMORY TO A POINTER USING new MAKE
SURE THAT U RELEASE IT USING delete. BCOZ LEAKING MEMORY
ISN'T GOOD FOR ANY PROGRAM AND MAY LEAD TO UN-TRAPPABLE
BUGS.

2. NEVER-NEVER USE AN UN-INITIALIZED POINTER. LIKE :
int *ptr;
cin >> *ptr;
cout << *ptr; // THIS WILL CRASH YOUR CODE AND IN SOME
CASES O/S AS WELL.
3. THOUGH U MAY INITIALIZE POINTERS TO NULL LIKE :
char *ptr = NULL;
MOST PROGRAMMERS DO THIS TO MAKE IT COMPLETELY CLEAR
THAT THIS POINTER IS NOT INITIALIZED WITH ANY VALID
ADDRESS. THOUGH THIS DOES NOT GUARANTEE THAT THIS PONTER
IS SAFE.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top