I often struggle to see the advantages of pointers (except for low level programming).
Why use of char* instead of a String or char[] or what advantages pointer arithmetic brings.
So what are the pros and use cases of pointers?
Software Engineering Stack Exchange is a question and answer site for professionals, academics, and students working within the systems development life cycle. It only takes a minute to sign up.
Sign up to join this communityI often struggle to see the advantages of pointers (except for low level programming).
Why use of char* instead of a String or char[] or what advantages pointer arithmetic brings.
So what are the pros and use cases of pointers?
Pointers are necessary for dynamic memory location, many data structures, and efficient handling of large amounts of data. Without pointers, you'd have to allocate all the program data globally or in functions or the equivalent, and you'd have no recourse if the amount of data grew beyond what you had originally allowed for. I hesitate to use absolutes here, but as far as I know all modern computer languages have pointers in some form or other.
In most languages that use pointers, there are certain sorts of references that are pointers, and perhaps certain sorts of references that aren't, and there is no further notational difference. A Lisp cons
cell is a pair of pointers, although a fixnum
is not a pointer. In Java, the variable used for the instance of a class is a pointer, but an int
isn't. The language syntax doesn't reflect that.
C is unusual in that pointers are optional, explicit, and allow explicit pointer arithmetic. It is perfectly possible to write struct foo bar; struct foo * baz;
, and once you've allocated memory for baz
you can use both bar
and baz
to represent struct foo
s. Since pointers are optional, it is useful to have notational differences. (It's essential in C++ for smart pointers, as given boost::shared_ptr<foo> bar;
, bar.reset()
has one meaning and bar->reset()
is likely to have a much different one.)
(Actually, explicit pointers were often used in other languages when C was originally being developed, such as ^
in Pascal. C is an older language than most in common use today, and it shows.)
One of C's design goals was to write Unix in, and therefore it needed to handle memory locations in a detailed manner. (C is actually one of a family of system implementation languages common when it was being designed, another example being Cybol for Control Data computers. C is the one that became a big hit.) Therefore, it is possible to manipulate C pointers directly, assigning memory addresses and calculating new ones. This also led to some design decisions in C. C arrays are based heavily on pointer arithmetic, and indeed an array decays into a pointer in very many situations. Passing variables to C functions by reference is done by pointer. There was no strong need for arrays and passing variables by reference in the form that other contemporary languages had, so C didn't get those.
So, the answer is that, in most languages nowadays, you use pointers constantly without being reminded of the fact. In C, and to a lesser extent C++, you use pointers either to do low-level things, or as accomplish higher-level things that there's no special notation for.
Complex data structures. You can't build something like a linked list or a binary tree without pointers.
There are no "pros" and "cons" of pointers. They are just a tool, like a hammer.
References in C++, Java and other same type of languages are just 'safe pointers'. And these references are used a lot in Java.
NULL
, and they cannot be modified after they are created.
– Billy ONeal
Nov 2 '10 at 13:16
Pretty much any computer program needs to inspect and change values in memory (known as peeking and pokeing, to those of us who are old enough). You need to control where in memory those values are, so that the outcome is predictable (and in certain cases, the order is important: loading executable code is one example). Therefore you need to have a data type that represents a location in memory. Even if your programming environment hides that under an abstraction, it's still there.
char*
is a crummy example of pointers. You are probably better off using std::string
(or some better type that handles your unicode/ansi/multibyte specialness) than char*
. For almost any other example of pointers (Employee*
, PurchaseOrder*
, ...), however, there are many advantages:
In fact, pointers are so important that most languages that appear not to have them actually have only them. Reference types in C# and Java are essentially pointers disguised as solid objects.
Now, pointer manipulation (p++
on a pointer, or p += delta
) is a whole 'nother story. Some people think it's dangerous, some people think it's great. But that's even further from your question.
Pointers can be faster and can incur less overhead, both in data structures and in keeping the program execution footprint down. (Please note the word 'can'.)
In general the rule is, if you allocated a resource, either by performing your own allocation or having something do it on your behalf, then it's your job to release it when done.
The burden of doing the above is placing the responsiblity back on to the developer, rather than having the runtime do it. This has some more advantages in that things can be longer lived, or cross boundaries, or be disposed of at more opportune times, or not need to carry the weight of a garbage collector around.
In exotic cases, usually involving exceptions and scope, there are some edge cases that require one to be a little more careful if the code the does cleanup gets avoided. Realistically, these cases can be designed around. We lived without managed code for many decades.
Often what makes pointers "hard" is merely not understanding what's going on at the hardware level. It's nothing more than indirection.
Pointers give you much more raw access, and this can be very helpful, clever, or necessary. You can point to anywhere and treat it pretty much as anything. If you use your God-like powers for good, it's very, very good.
The con side is usually waste by forgetting to release something, or by releasing it more than once, or referencing something after it's released, or refencing something when you're not pointing anywhere. These things often result in spectacular crashes, and to be honest are usually indicative that you've got a logic problem, rather than pointers are fragile.
If you're a solid developer, using pointers shouldn't be any more problematic than any other data structure. Again, it's not rocket science, and people did it for decades without even blinking an eye. It's just taught less throughly these days.
All that said, unless you have need for pointers, the conveinence and exoitic cases that a good garbage collection provides makes working in a managed environment that much nicer. It's great to be able to grab some memory, use it, and abandon it, knowing that at some time later, it might be discarderd, if it makes sense to do so. That's a bit less code on the coder's part, in exchange for a runtime that does some extra lifting.
The best answer is actually included in the question: pointers are for low-level programming. Granted, if you're using C, not using pointers is like programming with one hand tied behind your back, but the answer to that is to use a higher-level language instead.
Pointers provide a visibility into the machine that is required for most interesting programming. Most modern languages simply hide the gritty bits from you.