|
-
May 27th, 2009, 03:34 PM
#1
pointers--when to use them
I am a self-taught c++ (beginner?) I have been programming for a couple of months.
I have seen, in tutorials and the such, many uses of pointers. I understand basically what they do, etc. but do you really NEED them?
Can someone give me examples of cases in which pointers are much preferred to any other type of variable access? As far as I can tell, you might as well just use the variable names.
Also, I have seen code like this:
Why is the * used?
Thanks
-
May 27th, 2009, 03:36 PM
#2
Re: pointers--when to use them
Primarily when you want the object you're creating to exist outside of the scope where it was created and to take advantage of polymorphism.
-
May 27th, 2009, 04:08 PM
#3
Re: pointers--when to use them
This:
int my_int;
Declares an integer, called my_int.
This:
int * my_int;
Declares a pointer to an integer, called my_int. The * in this context indicates the type created will be a pointer to the type expression that precedes the *.
Frankly, pointers are often avoidable as raw pointers in modern C++.
However, the reason they're in the language is the fact that C, the root of C++, was originally implemented to be a kind of assembler, working very close to the level of the underlying CPU, and for that machine, pointers are a fact of it's operation. Languages that don't have pointers, like Java, aren't able to work at that level. Sometimes that's a benefit.
In the beginning phases of study you can just about ignore the pointer.
If you're going to handle images, audio, video or other forms of data like them, you're dealing with raw collections of data held in arrays. You can perform that work entirely in containers, and languages without pointers use analogous containers to operate with that data. In C++, however, you can achieve certain levels of performance that approaches the fastest that the underlying hardware can perform precisely because of constructs that work closely at the CPU's level, and in the case of image, audio and video data, that means pointers.
Another context, already pointed out, where pointers are important is in polymorphism. Java handles this through it's implementation as a reference, but in C++ we can use something called smart pointers to do something very similar. Still, the nature of polymorphic objects is that the type of an object may be more than at first is visible.
Say I have an object B, derived from A. If a function required an A, but I've created a B which has more functionality, it can still operate as an A. If the function took a COPY of an a, as in:
void function( A );
B myb;
Then:
function( myb );
would imply a copy is passed to function. But what about the part of the object that is a B? It can get lopped off.
If I pass a reference instead:
void function( A & );
then
function( myb );
would not pass a copy, is passes a reference - which is implemented, under the hood, as a pointer.
I could also pass a pointer:
void function( A * );
function( &myb );
This time I have to pass the address of myb, but still it's not a copy - and the function gets use of my entire object, including virtual functions in B that may offer unique behavior.
There's a lot more to it than just that, but this is one example.
If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).
-
May 27th, 2009, 05:25 PM
#4
Re: pointers--when to use them
Languages that don't have pointers, like Java, aren't able to work at that level.
It should be noted that from a certain point of view, all non-primitive objects in Java are referred to via pointers. They shouldn't be all that foreign to a Java programmer once they get over the fear of something different.
supahamster, if you have 4 or 10 or 15 objects you need to manage, you can get away with individual variable names for each. If you have a thousand objects, you can't.
One common way of dealing with that many is to throw them in an array. That works, but it has limitations. Fancier data structures may be more applicable. But no matter whether you use an array or something else....you're now referring to 1000 objects in your code with a single variable name.
Obviously there's no magic about it. That container, whatever it is, simply has a way to refer to all of the various objects internally. Not by individual names----same problem----but via a pointer or series of pointers which are arranged to form the data structure.
Read up on linked lists as a good example. Or simply realize that the name of an array is a pointer to the start of that array.
-
May 27th, 2009, 06:12 PM
#5
Re: pointers--when to use them
Can someone give me examples of cases in which pointers are much preferred to any other type of variable access? As far as I can tell, you might as well just use the variable names.
The main reasons I can think of offhand:
A. You need to create an object that can last beyond the local scope.
B. You need to create a dynamic number of something - you don't know the number until the program is actually running*.
C. You want to use polymorphism, which essentially requires the use of pointers.
* You can -and should- make use of things like std::vector in those situations. Ultimately, though, you're still making use of the capabilities of pointers, you just have vector doing all the dirty work for you.
-
May 28th, 2009, 01:08 AM
#6
Re: pointers--when to use them
 Originally Posted by supahamster
I have seen, in tutorials and the such, many uses of pointers. I understand basically what they do, etc. but do you really NEED them?
There are many algorithms, data structures and language mechanisms you simply cannot implement efficiently without the use of pointers. One important example is recursive data structures such as lists and trees. Another is parameter passing in functions.
The two most important uses of pointers are,
- to avoid extensive shuffling of data back and forth in memory, and
- to refer to memory allocated on the heap.
A different question is whether pointers must be an explicit feature of a programming language? Not necessarily. In C++ it is but in Java it is not.
Last edited by nuzzle; May 28th, 2009 at 01:41 AM.
-
May 28th, 2009, 10:01 AM
#7
Re: pointers--when to use them
Try this - http://learningcppisfun.blogspot.com...-function.html
It does not cover cross-language viewpoints but just scoped to C++ use of pointers and references - their step brothers - who people think that they hate each other. 
Pointer is just a way to implement a concept - which is "address of an object". How that concept gets implemented in a language depends on what things were considered during the language design. Some have it implemented as references and some using pointers. There may be other ways to implement that concept that I might not be aware of. If you ask for advantages of reference way of doing it as compared to pointers, well, references can go bad as well.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
May 28th, 2009, 10:13 AM
#8
Re: pointers--when to use them
 Originally Posted by nuzzle
There are many algorithms, data structures and language mechanisms you simply cannot implement efficiently without the use of pointers. One important example is recursive data structures such as lists and trees.
That is the biggest mental block that I have had for some time in past too and I see that to be a very common one. You ask someone to write a linked list and they will start with a Node structure with a data member and a pointer to next node. 
Pointers are not at all necessary to do that. If you consider the context of my last post above, you can do it using references as well. All you need is a way to refer-to/point-to the next object or to 2 or more nodes in case of tress. Pointers are one way, references are another but well yeah, there has to be a possibility of null references or a way to distinguish that. That is the USP of pointers, I guess, in C++. In C# or Java, the nullable-ness of references overcomes that.
 Originally Posted by nuzzle
The two most important uses of pointers are,
- to avoid extensive shuffling of data back and forth in memory, and
- to refer to memory allocated on the heap.
Not at all necessary, unless I misunderstood you and if you are willing to let go the USP of pointers as I mentioned above. Here is a way to point/refer to an object on the heap:
Code:
int& ref = *(new int(0));
If geniune null references were possible in C++, who knows, you might not have needed to the derefence I did above. new would have returned a reference directly, instead of a pointer. Interesting thought.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
May 28th, 2009, 10:23 AM
#9
Re: pointers--when to use them
In addition to NULLability, pointers have one other advantage over references: They can be reassigned.
-
May 28th, 2009, 10:35 AM
#10
Re: pointers--when to use them
 Originally Posted by Lindley
In addition to NULLability, pointers have one other advantage over references: They can be reassigned.
Not to mention pointer arithmetic
Old Unix programmers never die, they just mv to /dev/null
-
May 28th, 2009, 10:41 AM
#11
Re: pointers--when to use them
 Originally Posted by exterminator
In C# or Java, the nullable-ness of references overcomes that.
I regard references in C# and Java as pointers without pointer syntax. If null C++ references existed, then one benefit of C++ references over pointers would be gone... and references would become pointers with a different syntax. (Okay, and without pointer arithmetic, which admittedly is one difference between C# and Java references and pointers other than syntax.)
-
May 28th, 2009, 10:52 AM
#12
Re: pointers--when to use them
 Originally Posted by Lindley
In addition to NULLability, pointers have one other advantage over references: They can be reassigned.
Point taken, but if a reference could be NULL at one time and not NULL at one time, I thought that it would be implicit that references are reassignable. Anyways, that is what I had in mind when I was comparing with Java/C# references and it is just a hypothetical case of talking of changing the way C++ references work, which would be a non-trivial task, I feel.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
May 28th, 2009, 10:56 AM
#13
Re: pointers--when to use them
 Originally Posted by HighCommander4
Not to mention pointer arithmetic 
Ok. 
Do you mean to say that what can be achieved in C++ with pointer arithematic cannot be achieved in say C#/Java where you don't have pointers and hence pointer arithematic? Any example?
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
May 28th, 2009, 10:58 AM
#14
Re: pointers--when to use them
 Originally Posted by laserlight
I regard references in C# and Java as pointers without pointer syntax. If null C++ references existed, then one benefit of C++ references over pointers would be gone... and references would become pointers with a different syntax. (Okay, and without pointer arithmetic, which admittedly is one difference between C# and Java references and pointers other than syntax.)
Pointer arithematic only seems a necessity because of the way pointers are modelled in C or C++. When you talk of objectives being achieved with that in C++, are you saying something similar can't be done in say Java or C#? I don't think so. There might be a different way to do it, but surely it would be possible to do it. If you think no, then it would be nice to have an example.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
May 28th, 2009, 11:06 AM
#15
Re: pointers--when to use them
 Originally Posted by exterminator
When you talk of objectives being achieved with that in C++, are you saying something similar can't be done in say Java or C#? I don't think so.
I did not talk about objectives. Rather, I talked about pointer arithmetic. You simply cannot "increment" or "advance" a reference, or "subtract" references in C# and Java (and C++). Just because you can say, use iterators instead does not change that fact with respect to my assertion that references in C# and Java are pointers without pointer syntax. What I have in mind is that effect where you can pass an object reference to a function, and assigning to that object reference would not change the object reference in the caller since the reference itself is passed by value.
Last edited by laserlight; May 28th, 2009 at 11:10 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|