Pointers, what are they good for... - c++

Started by
19 comments, last by Erlog 22 years, 9 months ago
I''m learning C++ right now, and I''ve been through about 4 or 5 books looking for the answer to my question: What exactly are pointers good for. I have found not one use for them. Could someone please explain where they would be used and especially why they would be used. -Erlog-
Advertisement
???
Pointers are the heart.

Sharing data/objects between entities is done via pointers easily.
They are also required to do dynamic memory allocation.
(Well if you don''t use C for that)

-* So many things to do, so little time to spend. *-
-* So many things to do, so little time to spend. *-
Well you cannot dynamically create new objects without the use of pointers somehow.

Pointers also minimize overhead when making function calls, etc.

Lets say you have a large complex data structure with 50 variables. If you were to pass the entire structure to a function that operates on it, the calling function would need to copy all of the variables from that structure onto the stack and the called function would need to pop them all back off. This can make things come to a crawl in a hurry.

If you pass a pointer to the function instead, you are only pushing a 32-bit memory address onto the stack and the calling function accesses the data structure that already exists in memory using this address.

Also, if you wanted the function to make changes to the data in the structure that you are passing to it, it can do so easily using a pointer since you are passing the actual address of the current data instead of copying it.

Hope this makes some sense

Seeya
Krippy
Java and Basic are pointerfree
It could be argued that Java uses nothing BUT pointers (all objects are passed by reference, not by value).

That also makes an excellent case: pointers do not have to be explicitly mentioned in a language. It can be written so that you do not need to understand the concept of pointers to use them to good effect.

For some low-level programming, having actual access to the pointers (as a true memory address), and performing pointer arithmetic, can be very useful.


People might not remember what you said, or what you did, but they will always remember how you made them feel.
Mad Keith the V.
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
That is why they are slower and not a higher-level language like C\C++. Well refering to BASIC anyways, JAVA is a lot more advanced that BASIC but not as advanced as C++.
When i began learing the C\C++ language I thought the same thing when i started learning pointers. Why do i need to save the address of a variable when i have the variable right here. Now i dont know how i would write some programs without them. Especially when it comes to passing arrays to functions and creating new objects, pointer are invaluable. I will say pointers are one of the reasons C\C++ are so powerful. (but not the only reason)

"There is humor in everything depending on which prespective you look from."
"There is humor in everything depending on which prespective you look from."
JAVA isn''t less advanced than C++. (much more the contrary)
Java is argued to be C++ done right, although I disagree on that I can say that JAVA is very nice to code, and is much less a pain than C++.

They are different, but in their domains they are very advanced.

Yep forgot the problems of vars... but to me it''s data sharing ;p


-* So many things to do, so little time to spend. *-
-* So many things to do, so little time to spend. *-
thanks all, I think I finally understand when, where, how, and why to use pointers. Thanks mostly to krippy''s post, that one helped me the most although, just about everyone''s helped me a teensy weensy bit besides Ingenu''s post that didn''t answer any of the questions that I wrote about at all.

-Erlog-
Thanky!
Of course pointers are the heart and power of the language. They give you the ability to directly access and manipulate memory. But I feel that an even simpler analogy is more helpful to those just starting out who may not yet have delved into the darker layers of harware mechanics, digital logic, registers and stacks.

Imagine if you will that you are taking a class in school, and there is only one phhysical textbook available to all the students. When a student "A" needs to use the textbook, he or she must take it down to a copy center, and replicate it. They can then use their own copy. When they are done with it, they must destroy the copy. This already seems like a huge hassle, not to mention a big waste. But lets make it worse. Lets say that student "A" knows that student "B" also needs to utilize the book. It would make sense that "A" could just hand off his copy to "B" and tell "B" to destroy it when they are done with it. But this is not allowed. "B" must take "A''s" copy over to the copy center and replicate. Then both "A" and "B" must destroy their copies when done. The magnitude of this waste of time, paper, and effort grows exponentially as the number of total students increase, and the functional complexity of classwork increases. Pointers remove this kind of problem, in that the original textbook can be referenced/utilized by any student at any time without having to make copies. Simply, the textbook may be made available as a shared resource, or even a web page for that matter. But this does open up a can of worms. I will leave that matter for you to learn on your own.

One final thought.... become very familiar with your debugger. This is your friend, well most of the time atleast
while java doesn''t have pointers it has something similar enough that it should be called pointers. They call them references but they have more in common with C++ pointers than with C++ references.

as to the question, you need pointers. It should be obvious. What if you want a creature to know what map tile it is on? It needs a pointer. Using a copy would be wrong, and not just because of memory concerns. Pointers create relationships. You need to understand this if you ever want to be a programmer.

This topic is closed to new replies.

Advertisement