Fog Creek Software
Discussion Board




Why are pointers used?

I am curious as to why pointers are used and what are they good for.  What benefits do they have when writing a program?

Eugene
Friday, August 06, 2004

The question is way too vague to answer.

Tell you what -- you answer the question "Why are _numbers_ used in programs and what are their benefits?" Then we'll know what you're getting at.

Eric Lippert
Friday, August 06, 2004

Because they point to things.

I was going to write something really sarcastic here but it's just so basic I simply can't get down that low.

old_timer
Friday, August 06, 2004

Basically when you get down to machine instructions, you need to tell the CPU things like

"Here's a number Mr.CPU - the address of a memory location.  Now be a jolly good chap and get me the number that is stored in that location"

Data doesn't just come from thin air - you need 'pointers' to 'point' to memory locations where your data is.

*very basically*

Mike Weller
Friday, August 06, 2004



Because control freaks from back in the 1980's are used to writing software when memory was not a cheap, abundant commodity and want/wanted to preserve what little they had/have by referencing data by moving around a virtual, tiny stick with a pointer on the end instead of passing the actual object around in memory, which takes up more processing time and RAM.

.
Friday, August 06, 2004

A couple benefits of pointers that I can think of:

1. They prevent data duplication - they allow one instance of the data to be manipulated by many different parts of the program without duplicating the data.

2. Resource (RAM...) Preservation - Obviously this is the benefit derived from not duplicating your data.

3. Abstract memory addresses.  You need some concept to abstract the memory addresses and the instructions the underlying processor uses to deal with them.

Dave B.
Friday, August 06, 2004

Because new ones are very expensive.

ha
Friday, August 06, 2004

They provide a mechanism to support indirection.  This is important in making a program dynamic in the sense that the memory structures are detemined at runtime.

another point
Friday, August 06, 2004

Yeah if you've got 50,000 records of 10 MB each, it's better to just randomly copy them around than to refer to them indirectly.

How about a complicated structure like a digraph of some objects (an FSM representing a regular expression for example)?  What's a clear way of saying CurrentState = CurrentState->Transition(c) without resorting to pointer semantics?

Kalani
Friday, August 06, 2004

Wow that helps me a lot!  Thanks "another point" and "Dave B".  Those were good answers.  It's like a light all of a sudden just went off in my head. heh.

Eugene
Friday, August 06, 2004

I'm sorry if I was too vague, I didn't know how to word the question.

Eugene
Friday, August 06, 2004

Is this your homework?  I hope not.


Friday, August 06, 2004

Because every problem in computer science can be solved with another level of indirection.

Chris Tavares
Friday, August 06, 2004

Many people say that the use of such artefacts as pointer arithmetic is the result of deficiencies in C which require a workaround, and that is why students find them difficult to understand.

Stephen Jones
Friday, August 06, 2004

It's interesting to note that some languages that support pointers don't support dynamic memory allocation.  Take RPG IV for example.  It can't be used to dynamically allocate memory  but you can point to data with pointers so as not to duplicate it.

A guy
Friday, August 06, 2004

It's a way of filtering developers. Only 10 percent of the population are mentally capable of understanding pointers.

*p
Friday, August 06, 2004

Pointers are used for a couple of simple reasons 1) Providing indirection 2) Allowing one to pass a large parameter list to a function without needing to duplicate the whole data

Code Monkey
Friday, August 06, 2004

Pointers also come in handy when you want to pass a parameter whose type is unknown until runtime. You can pass an untyped pointer and then cast it to the types necessary down your code.

Egor
Friday, August 06, 2004

Memory addresses can be calculated (useful when dealing with arrays, strings, dynamic data structures or inheritance/polymorphism etc). The calculated address is stored in a pointer.  Getting back to the actual value is called de-referencing.

A pointer is nothing else but a variable holding a memory address that addresses indirectly either data or code.

At a more basic level, processors implement instructions that require address calculation and some level of indirection. For example a simple conditional jump jnz address1 is in fact:

if Z flag then
    increment instruction pointer
else
    set instruction pointer @ address1
end if
execute instruction @ [instruction pointer]

The next instruction is indirectly referred via the instruction pointer.

Without indirection computers would simply not work.

Dino
Friday, August 06, 2004

So far there isn't one thing on there that you can't do with a reference, rather than a pointer, I think the user was asking more along the lines of: What can a pointer do that a reference cant

/.
Friday, August 06, 2004

They are very useful when writing low level system code such as device drivers.  On some computer systems hardware devices have registers that are mapped to sections of memory.

Say, for example, your system has a UART with a read register at address 0x10000, a transmit register at 0x10001, and a control register at 0x10002.  Then you could write:

    /* Outline with various details missing */

    char *readReg = (char *)0x10000;
    char *writeReg = (char *)0x10001;
    char *cntlReg = (char *)0x10002;

    /* Read next character from the UART */
    nextChar = *readReg;

    /* Send character 'A' out the UART */
    *writeReg = 'A';

    /* Set a control bit */
    cntlReg |= BIT_MASK;

mackinac
Friday, August 06, 2004

Am I Gene?  Nope.

Haha
Friday, August 06, 2004

Nice troll /.

A pointer has to be used for dynamic memory allocation (malloc, new) which was already mentioned.  Other than that you can use pointers as alternatives to array notation.  (Increment and Decrement the pointer to traverse the array and add or subtract a certain amount to index the array.)

Me
Friday, August 06, 2004

I though /. was making a very good point.

After all a pointer is really just a "reference" to an object in memory. It just so happens that it's "referenced" by its address in memory (which if we want to be pedantic isn't its physical address anyway but some fake virtual address).

There isn't a fundamental reason for high-level programming lanugages to have C-style pointers.

Which reminds me. Why do C & C++ distinguish between '.' and '->' anyway? Why do we have to write "p->element()" when 'p' happens to be a pointer and p.element() otherwise. Why can't we just write "p.element()" and let the compiler sort it out, after all it knows what 'p' is.

_
Friday, August 06, 2004

You need to stop thinking in terms of high-level languages (where pointers might well seem a bit, well, pointless at a first glance), and think in terms of the hardware, just for a minute. This should give you the insight into why pointers are unavoidably necessary at the machine level.

Essentially, the computer has a bunch of data stored in RAM, and it needs some way to remember where pieces of data are stored, a way to tell it where in the RAM to retrieve a piece of data from, etc. Essentially pointers. Without pointers there'd be no way to tell the CPU where to store and retreive data. (Well, you'll typically have a small number of 'registers' to work with aswell, but that's kinda besides the point). If you do any low-level coding, say writing assembly code, you'll use indirection and pointers all the time as a matter of necessity.

From then you can think about  things like at which level should the idea of a pointer be abstracted away and hidden from the programmer? Languages like C and C++ let you play with raw(-ish) pointers, whereas languages like Python abstract the notion of a pointer by making 'references' to variables, essentially playing around dynamically with a symbol table. But real pointers are still used behind the scenes, if you look at the Python source code, for example.

Matt
Friday, August 06, 2004

"There isn't a fundamental reason for high-level programming lanugages to have C-style pointers. "

Errrr, the whole concept of interface-based programming is based on C-style pointers.

Skagg
Friday, August 06, 2004

> Why do C & C++ distinguish between '.' and '->' anyway?

In C++ a reference cannot be null, and a pointer can: so you must use a pointer instead of a reference, if you want to define it before you have anything for it to point to.

The other difference is that you can change what a pointer is poting to; whereas you define which object the reference is refering to when you define the reference, and cannot subsequently make it refer to some other object.

For example, the following is legal:

int* p = 0; //p is null
int a = 3;
p = &a; //p points to a
int b = 4;
p = &b; //p points to b

You cannot do the above with C++ references.

C only has pointers, not references.

Christopher Wells
Friday, August 06, 2004

so indulge my ignorance for a second Christopher, but what is the difference bettween
int* p = 0; //p is null
int a = 3;
p = &a; //p points to a
int b = 4;
p = &b; //p points to b

and

//JAVA
Integer p=null;
Integer a = new Integer(3);
p=a;
Integer b = new Integer(4)
p=b;
p.setInt(5); //or whatever the method is
System.out.println(b)

>>5

Same thing no?

/.
Friday, August 06, 2004

> Same thing no?

Yes I think so; and a C# 'reference' too is the moral equivalent of a pointer (caveat: C# types are either value types or reference types ... somewhat similar to the difference between "int" and "Integer" in Java).

But C++ makes a distinction between pointers and references ... perhaps because C++ doesn't distinguish between value and reference types.

Using references:

intt& a; //compiler error: reference to nothing
int b = 1;
int& c = b; //ok: c is a reference to b
int d = 2;
c = d; //this doesn't make c a reference to d ... instead it assigns d's value to the integer referenced by c, i.e. now b == 2

Using pointers:

int* a; //no compiler error
int b = 1;
int* c = &b; //ok: c is a pointer to b
int d = 2;
*c = d; //this doesn't make c a pointer to d ... instead it assigns d's value to the integer pointed to by c, i.e. now b == 2
c = &d; //this reassigns c so that it's now pointing to d instead of b

Christopher Wells
Saturday, August 07, 2004

> > Why do C & C++ distinguish between '.' and '->' anyway?

Also, when you define a C++ class you can overload its -> operator (but not its . operator). This is used for defining "smart pointer" classes: classes that you use like pointers, but which provide additional features such as reference counting the object that they're 'pointing' to and automatically deleting it).

Christopher Wells
Saturday, August 07, 2004

> int b = 1; int* c = &b;

Or "int* c = new int(1);" if you want the pointee to be allocated from the heap instead of on the stack.

Christopher Wells
Saturday, August 07, 2004

Matt, I work on embedded systems, write assembler regularly and have no problems with the concept of pointers and how they relate to the underlying hardware. But when the original poster asks "what benefit do pointers have when writing a pogram?" then frankly the answer is that most of the time they have no benefit, unless you're doing really low-level software.

You are right that without pointers there would no way to tell the CPU where to store and retrieve data, but it doesn't mean that you have to expose that detail to high-level programmers. After all you could argue that without ALU flags the CPU wouldn't be able to handle conditional execution.  But you don't see many languages exposing the CPU flags directly to the programmer, not even in C.

Besides in most large-scale operating systems dereferencing a C/C++ pointer isn't hitting the RAM directly. That's a fiction carried forward from the days of flat memory spaces. On most large systems, a pointers go through several layers of hardware indirection before it hits the RAM. And if you're unlucky you may need to wait many milliseconds while the OS fetches a page of virtual memory from disk before you can access the value referenced by your "raw pointer" .

Skagg & Christopher, Yes you are right. If you look at the world through the C/C++ lens (which many of us do every day) then pointers are essential.  Thankfully the programming world is slowly moving away from its reliance on unsafe pointers for all but the jobs that really require them. "Why are pointers used?"... because we've got used to them and haven't stopped to ask why!

_
Saturday, August 07, 2004

For the most part, pointers are used because the language forces you to use them to get the job done.  Some languages force it more than others -- it is almost impossible to write a nontrivial C program without pointers, but you could write a useful large Ada program without them.

In most cases, pointers should be avoided if you can accomplish the same task without them (unless avoiding pointers would overly complicate the code or hurt performance).

T. Norman
Saturday, August 07, 2004

Nice post, _, agreed.

Roose
Saturday, August 07, 2004

> "Thankfully the programming world is slowly moving away from its reliance on unsafe pointers for all but the jobs that really require them.

I don't get it: poiners (or references) seem to me to be fundamental to programming.

They're not language-specific, present in some languages but not others: if a class contains another class, the UML notation for example supports your specifying whether it's contained by value or by reference.

A program needs subroutines, subroutines need parameters, and those parameters need to be 'by reference'.

And a non-trivial program has non-trivial data structures: for example I have one screen which contains N graphs, each graph displays M waves and O markers, and this is implemented using containers for the various objects.

Yes you can make 'pointers' 'safer' than in C++: remove the ability to do reinterpret_cast; implement reference-counting and garbage collection; call things 'references' instead of 'pointers'; and implement a library of 'container' classes (which use pointers) so that high-level programmers don't need to implement them. They're still pointers.

I liked _another point_'s point: "They provide a mechanism to support indirection.  This is important in making a program dynamic in the sense that the memory structures are detemined at runtime." That said it all, I thought.


> Some languages force it more than others -- it is almost impossible to write a nontrivial C program without pointers, but you could write a useful large Ada program without them.

I don't know many languages. What language[s] should I look at, to see how it does away with pointers? Or if I look at Ada, for example, will I just say "Well it doesn't have pointers ... but it does have references, which serve the same purpose as pointers."?

Christopher Wells
Saturday, August 07, 2004

'_' : Well I didn't make any claims that pointers are a useful thing to have hanging around in a high-level language - I was just trying to explain why they must exist at some level. I guess it wasn't exactly clear whether the OP knew this or not.

But yeah, I can't immediately think of any task I've had in a high-level language which needed pointers as opposed to references. That said I've not done any C++ for ages...

Matt
Saturday, August 07, 2004

> Yes you can make 'pointers' 'safer' than in C++: remove the ability to do ...

... and pointer arithmetic (for example incrementing a pointer to get a pointer to the next element of an array).

Christopher Wells
Saturday, August 07, 2004

Chris, You have a bad case of C++ myopia. Of course you can't write meaningful C or C++ programs without pointers. But that doesn't mean that you can't write programs without pointers, in the C sense of the word 'pointers'.

> .. and pointer arithmetic (for example incrementing a pointer to get a pointer to the next element of an array).

Yes. Who needs the ability to increment the same unsafe pointer passed the end of the array too? The number one cause of buffer overruns and security problems.

High-level languages should abstract away raw pointers in the same way that they abstract registers and CPU flags. Let's hope that in 10 years time C and C++ are viewed in the same light that assembler is today.

_
Saturday, August 07, 2004

> Chris, You have a bad case of C++ myopia.

Can you even think of an object diagram (not a class diagram) without imagining pointers/references? That's my question.

> Let's hope that in 10 years time C and C++ are viewed in the same light that assembler is today.

I already view them in a similar light.

> High-level languages should abstract away raw pointers in the same way that they abstract registers and CPU flags.

I guess the closest I've come to a 'high-level language' then are SQL and XML: structured storage, with a flat API from which you extract arrays of whatever you want. Even so, when I think of 'foreign keys' that relate the records SQL tables, I see these as pointers ... and XML documents are trees of elements, where the branches that link the trunk to the leaves are another kind of pointer.

Christopher Wells
Saturday, August 07, 2004

SQL foreign keys and XML nodes are not pointers, because they are only logical representations of the relationships between different elements of data.  Shuffle around the data on disk or in memory, and without using a different foreign key value or XML path traversal you can still find the data you want.
If a foreign key actually represented the track and sector and byte offset of another piece of data, then it would be a pointer.

A pointer references a location in the hardware (or virtual machine) in which the program runs -- not a location in a logical data structure created or used by the program.  An array index is not a pointer, neither is an SQL foreign key, neither is a character's position within a string (even though pointers may be used to hold their values).  I can implement a linked list in Ada using an array without pointers, by having each node hold the index of the next node in the list.
Java references are the equivalent of pointers though.

Of course, behind the scenes the OS, hardware, and DB deal with the actual memory and disk locations and will resolve everything into actual pointers.  But that doesn't mean YOU are programming with pointers.

T. Norman
Saturday, August 07, 2004

>If a foreign key actually represented the track and
>sector and byte offset of another piece of data, then
>it would be a pointer.

If your RDBMS manages primary key generation for you, that value just might reflect the sector/track where the record is stored (OK, chances are that it doesn't, but the point is that the meaning of that value is opaque to you).

It's the same way with pointers.  In fact, if you think that you can take a pointer value, open up your computer case, and identify exactly where the value it points to is located, you don't know enough about what your OS does for you.

>Shuffle around the data on disk or in memory, and
>without using a different [reference value] ... you
>can still find the data you want.

Sounds like virtual memory. :)

Kalani
Saturday, August 07, 2004

> If a foreign key actually represented the track and sector and byte offset of another piece of data, then it would be a pointer.

I didn't mean it literally. What I meant was that they're like pointers, in that they serve a similar purpose: for example if I have some customerid value such as '3', then I can 'dereference' on this value to get the customer data that is being pointed to, using a deferencing operator such as "select * from customertable where customerid=3".

I suppose that a language only needs to remove pointer arithmetic (and, remove constructs such as "char *readReg = (char *)0x10000;") in order that its pointers be no longer 'real' pointers but instead be some kind of 'reference' or 'handle': because removing pointer arithmetic allows the system to do (for example) heap compaction.

Christopher Wells
Sunday, August 08, 2004

All right, if you want to stretch things you can say that ANYTHING that refers to anything else is a pointer.  But I would draw the distinction by evaluating whether the pointer values have meaning outside the machine (or virtual machine) in which the program is running.

A set of database tables can be migrated to a totally different type of machine, and all the data and its relationships will be maintaned without having to change any of the primary or foreign key values or do any special low-level operations. Customer ID 3 stays as 3. The same goes for a linked list implemented as an array in which the array indices are used to locate the next node.  However, a linked list implemented with pointers can't be reconstructed in another machine with the same pointer values unless that other machine has fundamental similarities in its memory architecture and you are able to exercise low-level control over where memory is allocated.

T. Norman
Sunday, August 08, 2004

> But I would draw the distinction by ...

Fair enough. Anyway, the reasons I use pointers is to ...

  1) Instantiate objects on the heap (i.e. objects that continue to live after the subroutine which allocated them has returned), and then dereference to access those objects

  2) pass objects by reference to subroutines

  3) get polymorphic (i.e. virtual) function behaviour

... in languages that don't support using references and containers and virtual functions to implement these features.

OP: say if you need an explanation of what benefits these features have when writing a program.

Christopher Wells
Sunday, August 08, 2004

>However, a linked list implemented with pointers can't
>be reconstructed in another machine with the same
>pointer values unless ...

Unless you're using a virtual memory system that distributes a swap over a set of computers.

At this point we're not talking about the value of the concept of pointers in programming but rather the value of different models of storage and reference-lifetimes.

Kalani
Tuesday, August 10, 2004

Pointers,
What a interesting topic. Pointers are used so that the computer can pass around a single value (the address of a list, array for eg.) instead of a huge array.
It is called effeciency.

Pieter Germishuys
Sunday, August 15, 2004

*  Recent Topics

*  Fog Creek Home