C++ pointers question

EvilManagedCare

Senior member
Nov 6, 2004
324
0
0
Hi, I am very much a beginner in terms of programming. The bulk of any education in regard to this has been Java based, but I have taken a course on C++ and am about to enter a computer science program. These courses were focused on familiarizing students with the language and teaching OOP concepts. There was little in the way of actual computer science discussed (for example, not much was said about what was going on under the hood. It was all very much high level).

So I am somewhat confused about pointers as used in C++. I understand what they are, but to the beginner, they can sometimes make code appear confusing and are difficult to follow (I emphasize in the case of the beginner). It is possible my understanding of pointers is misguided, so please indulge me on the following questions.

My questions:

1. Why use pointers to manipulate variables or attributes of objects instead of doing so directly? Wouldn't it be easier to just call the setters or getters of the objects?

2. Are there certain programming situations in which pointers are preferable?

Thanks in advance
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
Ok. First, read these two thread to make sure you have a good understanding:

http://forums.anandtech.com/me...=2233290&enterthread=y
http://forums.anandtech.com/me...=2237090&enterthread=y

Now, on to your specific questions.

Why use pointers to manipulate variables or attributes of objects instead of doing so directly? Wouldn't it be easier to just call the setters or getters of the objects?

I'll give you two basic answers to this question, and one advanced answer.

Answer 1: Size. Pointers are of fixed-size -- either 32 bits or 64 bits these days. Things that are pointed to can be arbitrarily large. Passing them around through function calls would take a lot of resources and time. The practice of passing pointers is known as pass-by-reference (a little confusing, because references exist in C++ as well), or sometimes as pass-though-memory.

Answer 2: Flexibility. Use of pointers allows programmers to create data structures that are sized dynamically in the course of execution. For instance, it isn't possible to know in advance how large a particular input file might be, but you want to make sure that your code can handle any size. Without a pointer-based data structure, the only option would be to allocate a huge amount of memory up-front, and then hope that it is enough to hold all inputs.

Answer 3 (advanced): Pointers are actually very close to the way real hardware works. In the Von Neumann execution model (aka virtually every commercial computer), the notion of memory is abstracted as a single array of bytes. Read and write operations manipulate this data in the course of execution. Memory addresses are the means by which particular locations in memory are named -- they have basically been exported to high-level languages (e.g. C++) as pointers. In fact, pointers are memory addresses, plus some type information that is used only by the compiler. In this respect, pointers, just like all other data in computers, are just numbers.

Are there certain programming situations in which pointers are preferable?

Absolutely -- nearly all programming prefers pointers of some sort or another. OP, you've already used them a lot in Java without knowing it, in fact (well, not exactly, but very close). Every time you instantiate 'Foo foo = new Foo()' in Java, well, foo is a pointer to an object of class Foo. This notion is pretty fundamental to object-oriented programming, and frankly, to pre-OO programming as well.

The only real difference between pointers in C++ and 'pointers' in Java is that C++ pointers are more dangerous -- they're treated as numbers (which they are, of course), and so you can do dangerous things with them. And of course, anything you 'new' in C++ you should 'delete' when you are done with it...
 

EvilManagedCare

Senior member
Nov 6, 2004
324
0
0
Wow, thanks for the great response, degibson. I think one reason I have not come to fully appreciate pointers is because I have not been involved in or authored any large, complex projects. The level at which the class I took was offered didn't involve any overly large solutions so resources were never an issue.

That was very helpful and I thank you again.
 

drebo

Diamond Member
Feb 24, 2006
7,034
1
81
Large projects, bah.

One of the ways that I finally got pointers to sink in was to create a linked list class. It was simple and easy to do and very small. A console application that added some stuff and then enumerated the list. Creating the list class was easy to. I even incorperated some operator overloads and what not.

When you're getting to know a language, you don't want to start with a large application. Find something you want to use (pointers, for instance) and build a small sample application just to get a feeling for how that particular concept, function, class, etc, works.

My advice would be to build a linked list class. That'll help you understand a bit more about pointers.
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
Originally posted by: drebo
Large projects, bah.

One of the ways that I finally got pointers to sink in was to create a linked list class. It was simple and easy to do and very small. A console application that added some stuff and then enumerated the list. Creating the list class was easy to. I even incorperated some operator overloads and what not.

When you're getting to know a language, you don't want to start with a large application. Find something you want to use (pointers, for instance) and build a small sample application just to get a feeling for how that particular concept, function, class, etc, works.

My advice would be to build a linked list class. That'll help you understand a bit more about pointers.

I second this, as an exercise. If you can't get it right, its also small enough that you could post it on the forums and have us help you out.
 

Net

Golden Member
Aug 30, 2003
1,592
3
81
imagine one scenario.

you have an array of 10000 items. and you want a function to be able to have access to that array.

1. If we pass in the entire array then a copy of the array is made on the stack. We can manipulate it in our function then return the changed copy of the array. lots of overhead. original array remains the same. we'll have to update it in a for loop or by some other means. more overhead.

2. We can pass in a pointer to the array. A copy of the pointer is on the stack (usually only the size of a word. look up meaning of word, byte and double word). When we access this copy of the pointer it will point to our original array. Hardly any overhead. Plus we are changing the original array.

Think of a pointer as a "link" to a portion in memory where your value is stored.

 

chronodekar

Senior member
Nov 2, 2008
721
1
0
If for some reason, you are uncomfortable with pointers (I was initially) I suggest you code with C#. .NET seems to have been designed avoiding pointers.

That's not to say pointers are non-existent in C#, they are just very VERY difficult to implement.
 

deveraux

Senior member
Mar 21, 2004
284
0
71
Originally posted by: drebo
Large projects, bah.

One of the ways that I finally got pointers to sink in was to create a linked list class. It was simple and easy to do and very small. A console application that added some stuff and then enumerated the list. Creating the list class was easy to. I even incorperated some operator overloads and what not.

When you're getting to know a language, you don't want to start with a large application. Find something you want to use (pointers, for instance) and build a small sample application just to get a feeling for how that particular concept, function, class, etc, works.

My advice would be to build a linked list class. That'll help you understand a bit more about pointers.

Another vote for this too! It was pretty much how I got my head around pointers.
 

hans007

Lifer
Feb 1, 2000
20,212
18
81
Well for one thing pointers are the basis of calling functions. Even in a language like java or c# they pass references, which are basically some abstraction of a pointer.

In C++ when you pass any complex object that is more than one word it is a pointer. pointers also can refer to objects on the heap , because you will run out of stack space just creating objects in c++.

That and pointers are really useful for interfaces and such. I suppose there no short answer. Its really just used everywhere, and I think is at the heart of mostly everything.

Pointers are just memory addresess. THe key thing to this is that a memory address is only one "word" . so thats a small quantity to push around everywhere.

 

sao123

Lifer
May 27, 2002
12,653
205
106
just one point that noone seemed to mention is... inheritance.
Inheritance allows you to take a class structure and make it better, while leaving the original intact, without having to rewrite the entire code again.

Lets look at a simple inheritance tree:


base class: ANIMAL
int NUMBER_LEGS
bool FURRY
string Color


derived class DOG
void BARK()

derived class CAT
void MEOW()

derived class HAMSTER
void PLAYWHEEL()


each of DOG, CAT & HAMSTER have all the properties of animal, but aded their own things to improve it.

now what does this have to do with pointers?
you can refer to a derived object from a base pointer...

SO... you can have an array of ANIMAL pointers, and the first one points to a DOG, the next a HAMSTER, the third and fourth CATS. So you dont have to create seperate arrays of derived objects, you can use BASE CLASS pointers.

This is very useful in higher level programming, but a simple concept to understand.
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
Hold your horses there sao123! I wouldn't want to have to explain virtual function indirection... :)