开发者

C(++) Pointers, needing clarification [duplicate]

This question already has answers here: 开发者_如何转开发 Closed 11 years ago.

Possible Duplicate:

What are the barriers to understanding pointers and what can be done to overcome them?

Probably the silliest question I'll be asking on here, but a year's worth of random googling about when the thought came onto my mind has gotten me nowhere.

Would someone care to explain to me the use of pointers in C(++), why they are useful, and the practical application of pointers?

What I already know:

  1. The & pointer, when preceding a variable name, changes the reference to refer to the variable's address in memory.
  2. Pointers save space on the stack.
  3. It's possible to get by without pointers, though libraries such as the standard library require basic knowledge of them, which severely limits what you can do if you choose not to learn them.

For those attempting to explain--if it helps at all--I have 5 years of amateur experience in Java, which--in practice--does not handle pointers the way C does (however, Java does utilize pointers--see the NullPointerException for proof of that).

Thanks in advance!


Every object "reference" in Java is actually a pointer. When you pass an object as a parameter to a method in Java, you're effectively passing a pointer to the object; the actual object is not copied or anything. Both the caller and callee can refer to the same object. This probably seems natural to you.

In contrast, in C++, by default if you pass an object to a method, the object is copied. If you change the object in the method, the original object in the caller remains unchanged. This should seem weird to you.

But if you use pointers in C++, then C++ objects act like Java objects. Objects retain their identity, instead of being constantly copied and destroyed. Whew!

References in C++ are sort of pointers in disguise; if you pass an object to a method by reference, then both the caller and callee refer to the same object, but you can't make the variable in the callee refer to a different object without damaging the original object.


There's no simple answer to your question. You'll just need to study it a while.

But let me say this: At the machine level, microprocessors work with memory and registers. Pointers are simply a feature that allows you to work directly with memory also. It's what the computer does anyway. And there are many ways this can make code more flexible, efficient, or easier to implement.

For someone who has used them for many years, they seem the most natural thing in the world. For languages like C#, they are still very much present even if the language assumes control over them.


Pointers do not save space on the stack in so much as that is their primary function.

Pointers are variables that store the address in memory of some piece of data. This address is stored in a variable much like you can use a variable i to store the index of some piece of data in an array. The i is not the data but it tells you where it can be found in the array. A pointer is not the data but it tells you where it can be found in memory.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜